0

In my current project, I have Actors that want to move around in an environment. Different Actors may have different movement Strategies, and I am injecting the Strategies as dependencies into the Actors as follows (language-independent):

actor = new Actor(new Strategy());

However, the strategy needs to be able to make decisions based on the Actor's state (like its current position, health, etc). Therefore, the Actor would need to be injected into the Strategy, and this is obviously bad design since it is circular dependency.

After reading this article, I attempted to extract some state information from Actor into a new class called State and now have a dependency model that looks like this:

state = new State();
strategy = new Strategy(state);
actor = new Actor(state, strategy);

This eliminates the circular dependency. However, in my project, Actor is derived from a library class which has state information already (position, health, etc). Therefore, it doesn't really make sense to extract a third State class when Actor already has that responsibility. All State would be doing is grabbing the state from Actor, so at worst they are still circularly dependent, or at best they are strongly coupled.

What is the best way to handle a situation like this? In the end, I'm trying to inject movement strategies into Actors where the strategies need to know some state information to make smart decisions, and Actor extends a library class which contains state.

Auroratide
  • 2,299
  • 10
  • 15

1 Answers1

0

Strategy isn't really something explicitly owned by any particular Actor. It should probably be a singleton that takes Actor as a method parameter.

In C# this allows you to implement MEF to its fullest.

shadofx
  • 36
  • 2