Java Reference
In-Depth Information
Chapter 23. Strategy
A strategy is a plan, or approach, for achieving an aim, given certain input conditions.
A strategy is similar to an algorithm: An algorithm is a well-defined procedure that produces
an output from a set of inputs. A strategy is a plan that pursues an output given a set of inputs.
Usually, however, a strategy has more latitude in how to pursue its goal than an algorithm
does. This latitude also means that strategies often appear in groups, or families, of
alternatives. When multiple strategies are available, the logic that surrounds the strategies
must select one and then execute it. Complexity in the selection and use of strategies can lead
to complex and tangled code. You can clean up such code with the S TRATEGY pattern.
The intent of S TRATEGY is to encapsulate alternative strategies, or approaches, in separate
classes, each of which implements a common operation. The strategic operation defines the
inputs and output of a strategy but leaves implementation up to the individual classes. Classes
that implement the various approaches implement the same operation and are thus
interchangeable, presenting the same interface to clients.
Modeling Strategies
Consider the Oozinoz advertising policy that suggests a firework to purchase when the
customer is visiting the Oozinoz Web site or calling into the call center. Oozinoz uses two
commercial off-the-shelf recommendation engines to help choose the right firework to offer a
customer. The Customer class chooses and applies one of these engines to decide on which
firework to recommend to a customer.
One of the recommendation engines, the Rel8 engine, suggests a purchase based on the
customer's similarity to other customers. For this recommendation to work, the customer must
have registered and given information about likes and dislikes about fireworks and other
entertainments.
If the customer has not registered, Oozinoz uses LikeMyStuff , another vendor's engine,
that suggests a purchase based on the customer's recent purchases. If data is insufficient for
either engine to function, the advertising software picks a firework at random. However, a
special promotion can override all these considerations to promote a specific firework that
Oozinoz wants to sell. Figure 23.1 shows the classes that collaborate to suggest a firework to
a customer.
Search WWH ::




Custom Search