402 | Chapter 11: Strategy Pattern
between thecontextclasses and the strategy classes. The context classes are made up
of a class and its subclasses. The context classes delegate actions to the strategy
classes, made up of strategy interfaces and implementations of those interfaces. So
the context classes delegate to the strategy classes.
The delegation process begins in the main context class. It includes reference vari-
ables to the strategy interfaces. Methods in the context class do the actual delega-
tion. The concrete context classes inherit the instance variables from the context
class, and use those variables to specify which behaviors they want from the con-
crete strategies. Figure 11-2 shows a visual depiction of delegation.
If an instance of a class, which has a reference to a delegate that is called, is called,
the delegate invokes the called method containing the encapsulated algorithm. So
instead of inheriting the operation, the object can be said tohave amethod that han-
dles a call.
Minimalist Abstract State Pattern
This first example cuts to the bare bones of a Strategy design pattern to reveal the
structure of the design pattern. Each part is analyzed in terms of what it does for the
overall pattern. This example should help make the structure clearer.
You need to see the Strategy design pattern in a context where several different
objects delegate behaviors to different delegates. (The second example provides this
richer applicability.) In this minimalist example, you need to look at the points of
delegation and how it all works. In Figure 11-3, following the listing, you will be able
to see what looks like a web of intrigue. Not to worry; the illustration just shows the
delegation work going on between the objects in the application.
Figure 11-2. Delegation
Request goes to Object Z, but Object Z farms
it out (delegates it) to Delegate Beta
Aggregator
Object Y Object Z
Delegate
Delegate
Beta
Delegate
Gamma
Delegate
Alpha