You are on page 1of 1

Strategy

Monday, July 20, 2009


8:57 AM

Pattern
• Strategy is a behavioral pattern
• Strategy decouples behavior of a class from the class
• Strategy is an alternative to Template method.

Metaphor
• Modes of transportation to an airport is an example of a Strategy.
Several options exist such as driving one's own car, taking a taxi, an airport shuttle, a city bus, or a limousine service.
For some airports, subways and helicopters are also available as a mode of transportation to the airport.
Any of these modes of transportation will get a traveler to the airport, and they can be used interchangeably.
The travel agency will, on the behalf of the traveler, pick the type of transportation based on based on tradeoffs between cost, convenience, and time.

• When I was a young boy, I used to have a Mattel Superstar airplane that had a little plastic disk inside.
The grooves and notches on the disk made the airplane fly in a given pattern.
By changing the disk the plane held, I'd get a different flight path.
In this analogy, the Plane is the Context object, and the disks are implementation of the Strategy Object.
The size of the disks and the shape/size of the hole at their center is the interface of the Strategy.

• How do you sort the contact list in your mobile phone? By surname, by family name or maybe even by company.
These are different strategies to sort your contact list.

Typical Applications
• The easiest way to determine whether a strategy pattern will be needed is during the reading
of the requirements. If the requirements ask for some logic or some other logic ('or' being the important part), you probably have a very good candidate for the strategy pattern.
Likewise, if you find yourself thinking that there are two or more ways of accomplishing some task, a strategy pattern is likely needed.
• If classes differ only in their behavior. Then it's a good idea to isolate the algorithms in separate classes in order to have the ability to select different algorithms at runtime.
The checklist :
i. Identify an algorithm (i.e. a behavior) that the client would prefer to access through a "flex point".
ii. Specify the signature for that algorithm in an interface.
iii. Bury the alternative implementation details in derived classes.
iv. Clients of the algorithm couple themselves to the interface.

Structure

Implementation issues
• The context object receives requests from the client and delegates them to the strategy object.
Just as an example :
RobotContext r1 = new RobotContext("Big Robot");
r1.setBehaviourStrategy(new AgressiveBehaviour()); r1.move();
r1.setBehaviourStrategy(new DefensiveBehaviour()); r1.move();
r1.setBehaviourStrategy(new NormalBehaviour()); r1.move();
This could be a way of using the strategy pattern. This is however not the right way!
The implementation should support the generic value of "maximize cohesion and minimize coupling" In this example far too manydetails of the implementation are exposed to the client.
In strategy however the client isn't the one in charge, that's the context. Therefore the context object should contain thelogic to differ between the different algorithm. This shouldn't be left to the client.
By doing so the client has only to send a parameter (like a string) to the context asking to use a specific algorithm. But it's up to the context object to decide which one is actually returned.
RobotContext r1 = new RobotContext("Big Robot");
r1.setBehaviourStrategy("Agressive"); r1.move();
r1.setBehaviourStrategy("Defensive"); r1.move();
r1.setBehaviourStrategy("Normal"); r1.move();

Drawbacks
• The context object will not use all the information passed to them.
A possible solution is to implement a tighter coupling between Strategy and Context.
• The strategy pattern is limited to the invocation of just one polymorphic function on an object.
But what if you require several polymorphic functions on an object. As a practical example : what if you would like to change both the format and the output style of your logging mechanism.
The strategy pattern doesn't help you out but bridge pattern does. Therefore use bridge pattern when more polymorphic functions are involved.

GOF Patterns Pagina 1

You might also like