Search code examples
design-patternsstrategy-pattern

Strategy Pattern with derived alogrithms calling methods on context


I am in need for some design help. I have a class, let's call it a spaceship which can implement several behaviors. I've defined an interface to declare the operations each behavior supports. Until now this is the classic strategy pattern.

However, the implemented strategies need to be able to call actions and methods on the context class. I've been thinking of passing along the context as an interface itself to the encapsulated algorithms.

Is this the best approach? If not, what would you recommend?

enter image description here


Solution

  • What you are planning to do seems fine to me. One thing to hold into account is to not pass in too much to the method(s) of your behaviour class(es). For instance, if your spaceship has an engine behaviour, only pass it your fuel tank and drive unit, not the whole spaceship.