Strategy
Consequences
The Strategy pattern has the following benefits and drawbacks:
- Families of related algorithms.
Hierarchies of Strategy classes define a family of algorithms or
behaviors for contexts to reuse. Inheritance can
help factor out common functionality of the algorithms.
- An alternative to subclassing.
Inheritance offers another way to support a variety of algorithms or
behaviors. You can subclass a Context class directly to give it
different behaviors. But this hard-wires the behavior into Context.
It mixes the algorithm implementation with Context's, making Context
harder to understand, maintain, and extend. And you can't vary the
algorithm dynamically. You wind up with many related classes whose
only difference is the algorithm or behavior they employ.
Encapsulating the algorithm in separate Strategy classes lets you vary
the algorithm independently of its context, making it easier to
switch, understand, and extend.
- Strategies eliminate conditional statements.
The Strategy pattern offers an alternative to conditional statements for
selecting desired behavior. When different behaviors are lumped into one
class, it's hard to avoid using conditional statements to select the
right behavior. Encapsulating the behavior in separate Strategy classes
eliminates these conditional statements.
For example, without strategies, the code for breaking
text into lines could look like
void Composition::Repair () {
switch (_breakingStrategy) {
case SimpleStrategy:
ComposeWithSimpleCompositor();
break;
case TeXStrategy:
ComposeWithTeXCompositor();
break;
// ...
}
// merge results with existing composition, if necessary
}
The Strategy pattern eliminates this case statement by delegating the
linebreaking task to a Strategy object:
void Composition::Repair () {
_compositor->Compose();
// merge results with existing composition, if necessary
}
Code containing many conditional statements often indicates
the need to apply the Strategy pattern.
- A choice of implementations.
Strategies can provide different implementations of the same
behavior. The client can choose among strategies with different
time and space trade-offs.
- Clients must be aware of different Strategies.
The pattern has a potential drawback in that a client must understand
how Strategies differ before it can select the appropriate one.
Clients might be exposed to implementation issues. Therefore you
should use the Strategy pattern only when the variation in behavior is
relevant to clients.
- Communication overhead between Strategy and Context.
The Strategy interface is shared by all ConcreteStrategy classes
whether the algorithms they implement are trivial or complex. Hence
it's likely that some ConcreteStrategies won't use all the information
passed to them through this interface; simple ConcreteStrategies may
use none of it! That means there will be times when the context
creates and initializes parameters that never get used. If this is an
issue, then you'll need tighter coupling between Strategy and Context.
- Increased number of objects.
Strategies increase the number of objects in an application. Sometimes
you can reduce this overhead by implementing strategies as stateless
objects that contexts can share. Any residual state is maintained by the
context, which passes it in each request to the Strategy object. Shared
strategies should not maintain state across invocations. The
Flyweight pattern describes this approach in more
detail.