views:

129

answers:

3

Of course you can regard it as a kind of strategy, but that applies to almost all the design patterns. So : why?

+3  A: 

Because some of the components implement strategies, "ways to achieve something".

The point of the strategy pattern is to let you choose a way of doing something, a "strategy", at runtime.

The name doesn't refer to the pattern as a whole, it refers to the objects within the pattern that perform actions.

RichieHindle
Sounds more like a "tactic" at this point, no? Wouldn't a "strategy" imply a bigger "master plan"-like paradigm?
jldupont
@jldupont: Don't blame me, I didn't name it! :-)
RichieHindle
Yes, but so much of them do , factory for example..
Peter
Yes, I accepted it cause I guess you are right, but this is indeed a lame name :-)
Peter
I am not blaming anybody of course: I am just suggesting that maybe the interpretation isn't quite adequate.
jldupont
+3  A: 

The idea behind the strategy pattern is that "algorithms can be selected at runtime." (Wikipedia, Strategy Pattern) The pattern, in essence, is selecting the right strategy (or behavior) for solving a particular problem at runtime. Hence, it's name.

JasCav
Feels like a more appropriate definition than the one provided by @RichieHindle.
jldupont
+1  A: 

It is true that all patterns are "strategies" for application design. The Strategy pattern though takes complicated logic, algorithms, etc. and encapsulates it. Which cleans your code up and also allows different strategies to be used based on different conditions.

Ex. I wrote a complicated interest calculation for Collateral and instead of actually having the calculation inside of the Collateral class I moved it into a InterestCalcuation (strategy) class. Then when they started telling me the calculation need to be different, because the InterestCalculation adhered to the IInterestCalculation interface I could switch out and create new calculations as much as I needed and the Collateral class was unaffected...

J.13.L