- Define a family of algorithms, encapsulate each one, and make them interchangeable.
- Strategy lets the algorithm vary independently from clients that use it.
- Policy
- Classes differ only in their behavior.
- Different variants of an algorithm.
- Algorithm uses data that clients shouldn't know about.
- A class defines many behaviors.
Strategy
- declares an interface common to all supported algorithms.
- Context uses this interface to call the algorithm defined by a ConcreteStrategy.
ConcreteStrategy
- implements the algorithm using the Strategy interface.
Context
- is configured with a ConcreteStrategy object.
- maintains a reference to a Strategy object.
- may define an interface that lets Strategy access its data.
- 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 request from its clients to its strategy.
- Client 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.
- Families of related algorithms.
- An alternative to subclassing.
- Strategies eliminate conditional statements.
- A choice of implementations.
- Clients must be aware of different Strategies.
- Communication overhead between Strategy and Context.
- Increased number of objects.
- Compositor
- SimpleCompositor, TeXCompositor, ArrayCompositor
- Composition
No comments:
Post a Comment