Java Reference
In-Depth Information
data caching strategies. In the business arena, the Strategy pattern is sometimes used to represent different
possible approaches to performing business actions. Placing an order for a workstation, for example, might be
implemented as a Strategy if processing an order that had to be custom built was significantly different from
processing one that was based on a standard product model.
Like the State pattern (see “ State ” on page 104), Strategy decouples part of a component into a separate group of
classes. Part of a component's behavior is delegated to a set of handlers.
Benefits and Drawbacks
Each behavior is defined in its own class, so the Strategy leads to more easily maintainable behaviors. It also
becomes easier to extend a model to incorporate new behaviors without extensive recoding of the application.
The primary challenge in the Strategy pattern lies in deciding exactly how to represent the callable behavior. Each
Strategy must have the same interface for the calling object. You must identify one that is generic enough to
apply to a number of implementations, but at the same time specific enough for the various concrete Strategies
to use.
Implementation
The Strategy class diagram is shown in Figure 2.14 .
Figure 2.14. Strategy class diagram
To implement the Strategy pattern, use the following:
StrategyClient - This is the class that uses the different strategies for certain tasks. It keeps a reference to the
Strategy instance that it uses and has a method to replace the current Strategy instance with another Strategy
implementation.
Strategy - The interface that defines all the methods available for the StrategyClient to use.
ConcreteStrategy - A class that implements the Strategy interface using a specific set of rules for each of the
methods in the interface.
Pattern Variants
None.
Related Patterns
Related patterns include the following:
Singleton (page 34) - Strategy implementations are sometimes represented as Singletons or static resources.
Flyweight (page 183) - Sometimes Strategy objects are designed as Flyweights to make them less expensive to
create.
Factory Method (page 21) - The Strategy pattern is sometimes defined as a Factory so that the using class can
use new Strategy implementations without having to recode other parts of the application.
 
Search WWH ::




Custom Search