So what is the strategy pattern? According to Gang of 4 (GOF) strategy pattern's intent is:
* Define a family of algorithms, encapsulate each one, and make them interchangeable
* Let the algorithm vary independently from clients that use it
Can you think of examples when to use the strategy pattern?
• Many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors.
• You need different variants of an algorithm. For example, you might define algorithms reflecting different space/time trade-offs.
• An algorithm uses data that clients shouldn't know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures.
• A class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.
so even "any related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors." is an objective ?
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop