Applied Design Patterns with Java
Behavioral :: Strategy (315) {C ch 24}
Applicability
Use the Strategy pattern in either of the following cases:
- many related classes differ only in their behavior.
Strategies provide a way to configure a class with one of many behaviors.
- there is a need different variants of an algorithm.
For example, there is a need to define algorithms reflecting different space/time trade-offs. Strategies
can be used when these variants are implemented as a class hierarchy of algorithms.
- 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.
Structure
Participants
- Strategy
(Compositor) declares an interface
common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.
- ConcreteStrategy (SimpleCompositor, TeXCompositor,
ArrayCompositor) implements the algorithm using the Strategy interface.
- Context (Composition)
is configured with a ConcreteStrategy object, maintains a reference to a Strategy object, and may define an interface
that lets Strategy
access its data.
Catalog Behavioral Prev Next