The Strategy Pattern Flashcards
What is the intent of the Strategy Pattern ?
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
What is the motivation of the Strategy Pattern ?
Many algorithms exist for breaking a stream of text into lines. Hard-wiring all such algorithms into the classes that require them isn’t desirable for several reasons:
- Clients that need linebreaking get more complex if they include the line-breaking code. That makes clients bigger and harder to maintain, especially if they support multiple linebreaking algorithms.
- Different algorithms will be appropriate at different times. We don’t want to support multiple linebreaking algorithms if we don’t use them all.
- It’s difficult to add new algorithms and vary existing ones when linebreaking is an integral part of a client.
We can avoid these problems by defining classes that encapsulate different line-breaking algorithms. An algorithm that’s encapsulated in this way is called a strategy.
What is the applicability of the Strategy Pattern ?
Use the Strategy pattern when
- 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. Strategies can be used when these variants are implemented as a class hierarchy of algorithms [HO87].
- 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.
What are the participants of the Strategy Pattern structure ?
- The Strategy Interface
- The Concrete Strategies
- The Context
What is the role of the concrete strategies participants in the Strategy pattern structure ?
implements the algorithm using the Strategy interface.
What is the role of the strategy interface participant in the Strategy pattern structure ?
declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.
What is the role of the Context participant in the Strategy pattern structure
- is configured with a ConcreteStrategy object.
– maintains a reference to a Strategy object.
– may define an interface that lets Strategy access its data.
Describe the collaborations between the Strategy pattern participants ?
- Strategy and Context interact to implement the chosen algorithm. A context may pass all data required by the algorithm to the strategy when the algorithm is called. Alternatively, the context can pass itself as an argument to Strategy operations. That lets the strategy call back on the context as required.
- A context forwards requests from its clients to its strategy. Clients usually create and pass a ConcreteStrategy object to the context; thereafter, clients interact with the context exclusively. There is often a family of ConcreteStrategy classes for a client to choose from.
What are the benefits and drawbacks of the Strategy Pattern ?
- Families of related algorithms.
- An alternative to subclassing.
- Strategies eliminate conditional statements.
- A choice of implementations.
- Clients must be aware of different Strategie
- Communication overhead between Strategy and Context.
- Increased number of objects.
What are the implementation challenges/issues of the implementation of the strategy pattern ?
- Defining the Strategy and Context interfaces. Specifically how data are passed between both.
- Strategies as template parameters.if (1) the Strategy can be selected at compile-time, and (2) it does not have to be changed at run-time.
- Making Strategy objects optional. The Context class may be simplified if it’s meaningful not to have a Strategy object. Context checks to see if it has a Strategy object before accessing it. If there is one, then Context uses it normally. If there isn’t a strategy, then Context carries out default behavior.