Modified Strategy Design Pattern
Posted
by
Samuel Walker
on Programmers
See other posts from Programmers
or by Samuel Walker
Published on 2011-03-13T12:49:07Z
Indexed on
2011/03/13
16:17 UTC
Read the original article
Hit count: 450
I've started looking into Design Patterns recently, and one thing I'm coding would suit the Strategy pattern perfectly, except for one small difference.
Essentially, some (but not all) of my algorithms, need an extra parameter or two passed to them.
So I'll either need to
- pass them an extra parameter when I invoke their calculate method
or
- store them as variables inside the ConcreteAlgorithm class, and be able to update them before I call the algorithm.
Is there a design pattern for this need / How could I implement this while sticking to the Strategy Pattern?
I've considered passing the client object to all the algorithms, and storing the variables in there, then using that only when the particular algorithm needs it. However, I think this is both unwieldy, and defeats the point of the strategy pattern.
Just to be clear I'm implementing in Java, and so don't have the luxury of optional parameters (which would solve this nicely).
© Programmers or respective owner