Strategy: A Behavioral Design Pattern: by Anne Ryan
Strategy: A Behavioral Design Pattern: by Anne Ryan
By Anne Ryan
What is a Design Pattern?
Context Strategy
contextInterface() algorithmInterface()
Class
functionX()
SubClass2.1 SubClass2.2
behaviorY() behaviorY()
Strategy makes this easy!
StrategyX
functionX()
Class
functionX()
functionY()
...
StrategyY
functionY()
...
Benefits of Strategy
Eliminates conditional statements
Can be more efficient than case statements
Choice of implementation
Client can choose among different
implementations with different space and time
trade-offs
Benefits of Strategy
Families of related algorithms
Alternative to subclassing
This lets you vary the algorithm dynamically,
which makes it easier to change and extend
You also avoid complex inheritance structures
Drawbacks of Strategy
Clients must be aware of different
strategies
Clients must know how strategies differ so it
can select the appropriate one
Communication overhead between strategy
and context
Sometimes the context will create and
initialize parameters that are never used
Drawbacks of Strategy
Increased number of objects
if the algorithm differences are simple, the
extra classes add extra complexity
Implementation Issues
Context Strategy
contextInterface() algorithmInterface()