Tutorial :Why is the strategy pattern called the strategy pattern?


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


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.


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.


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...

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »