How to implement Strategy pattern for combined behaviours

by JorgeDLuffy   Last Updated May 02, 2018 18:05 PM

Strategy pattern solves the necessity of applying a certain algorithm/behaviour depending on the object's type itself. So you can iterate over a bunch of similar objects and call the same function expecting different algorithms to be executed for each one.

But what about two (or more) objects may have a special algorithm for them together? Only if they both are in our list to iterate. If not, they have their own single object behaviour.


We have different object types: Blue, Yellow and Red. They all have their own shine() method, which implements from its own strategy: BlueShineStrategy, YellowShineStrategy and RedShineStrategy.

Then I have a combination of Blue and Yellow objects. In order to make them shine, I'll do:

combination = [blueObject, yellowObject]
for c in combination:

That's okay. The problem comes when a restriction is added. If Red and Blue are present in the combination, I cannot call the shine method for everyone, I must use, just for those two, a RedBlueShineStrategy.

When is the time to ask and decide the use of the combined strategies? Is it in the moment of execution (the for moment)?

Or should I decide this before? (considering I can model the combinations, I can have an array of "special groups" included in the model)

Related Questions

Implementing strategy pattern via configuration

Updated February 28, 2018 08:05 AM

Strategy pattern and different implementation

Updated March 08, 2019 15:05 PM