Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 876
vondip Avatar asked Feb 10 '26 15:02

vondip


1 Answers

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.

like image 113
Jeroen De Dauw Avatar answered Feb 14 '26 22:02

Jeroen De Dauw