Download presentation
Presentation is loading. Please wait.
Published byLesley Foster Modified over 9 years ago
1
Strategy Design Patterns CS 590L - Sushil Puradkar
2
Intent Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Behavioral pattern Behavioral pattern AKA: Policy AKA: Policy
3
Motivation Hard-wiring all algorithms into a class makes the client more complex, bigger and harder to maintain. We do not want to use all the algorithms. It’s difficult to add and vary algorithms when the algorithm code is an integral part of a client.
4
Applicability Many related classes differ only in their behavior. Many related classes differ only in their behavior. You need different variants of an algorithm. Strategy can be used as a class hierarchy of algorithms. You need different variants of an algorithm. Strategy can be used as a class hierarchy of algorithms. An algorithm use data structures that clients shouldn’t know about. An algorithm use data structures that clients shouldn’t know about. A class defines many behaviors, and these appear as multiple conditionals in its operation. A class defines many behaviors, and these appear as multiple conditionals in its operation.
5
Example 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. Any of these modes of transportation will get a traveler to the airport, and they can be used interchangeably. The traveler must chose the Strategy based on tradeoffs between cost, convenience, and time.
6
Example Many algorithms exists for breaking a string into lines. Simple Compositor is a simple linebreaking method. Simple Compositor is a simple linebreaking method. TeX Compositor uses the TeX linebreaking strategy that tries to optimize linebreaking by breaking one paragraph at a time. TeX Compositor uses the TeX linebreaking strategy that tries to optimize linebreaking by breaking one paragraph at a time. Array Compositor breaks a fixed number of items into each row. Array Compositor breaks a fixed number of items into each row.
7
Structure
8
Participants Strategy Strategy declares an interface common to all supported algorithms. Context uses its interface to call the algorithm defined by a ConcreteStrategy. ConcreteStrategy ConcreteStrategy implements a specific algorithm using the Strategy interface. Context Context –is configured with a ConcreteStrategy object. –maintains a reference to a Strategy object. –may define an interface for Strategy to use to access its
9
Consequences Families of related algorithms Families of related algorithms – Hierarchies of Strategy factor out common functionality of a family of algorithms for contexts to reuse. An alternative to subclassing An alternative to subclassing – Subclassing a Context class directly hard-wires the behavior into Context, making Context harder to understand, maintain, and extend. – Encapsulating the behavior in separate Strategy classes lets you vary the behavior independently from its context, making it easier to understand, replace, and extend. Strategies eliminate conditional statements. Strategies eliminate conditional statements. – Encapsulating the behavior into separate Strategy classes eliminates conditional statements for selecting desired behavior. A choice of implementations A choice of implementations – Strategies can provide different implementations of the same behavior with different time and space trade-offs.
10
Consequences (cont..) Clients must be aware of different strategies. Clients must be aware of different strategies. –A client must understand how Strategies differ before it can select the appropriate one. –You should use the Strategy pattern only when the variation in behavior is relevant to clients. Communication overhead between Strategy and Context. Communication overhead between Strategy and Context. –The Strategy interface is shared by all ConcreteStrategy classes. –It's likely that some ConcreteStrategies will not use all the information passed to them through this common interface. –To avoid passing data that get never used, you'll need tighter coupling between Strategy and Context. Increased number of objects Increased number of objects –Strategies increase the number of objects in an application. –Sometimes you can reduce this overhead by implementing strategies as stateless objects that context can share. –The Flyweight pattern describes this approach in more detail.
11
Known Uses EventListeners in Java EventListeners in Java Layout managers in Java Layout managers in Java Calculating financial instruments Calculating financial instruments
12
Relater Pattern Flyweight: Strategy objects often make good flyweights. Flyweight: Strategy objects often make good flyweights.Intent Use sharing to support large number of fine-grained objects efficiently.
13
References New Even Handling in Java http://www.nada.kth.se/~li/Dais/L7.pdf New Even Handling in Java http://www.nada.kth.se/~li/Dais/L7.pdf http://www.nada.kth.se/~li/Dais/L7.pdf Examples of Design Patterns http://www.agcs.com/supportv2/techpapers/patterns/ papers/patexamples.htm Examples of Design Patterns http://www.agcs.com/supportv2/techpapers/patterns/ papers/patexamples.htm http://www.agcs.com/supportv2/techpapers/patterns/ papers/patexamples.htm http://www.agcs.com/supportv2/techpapers/patterns/ papers/patexamples.htm http://selab.korea.ac.kr/selab/courses/GoF-patterns http://selab.korea.ac.kr/selab/courses/GoF-patterns http://selab.korea.ac.kr/selab/courses/GoF-patterns Eample of Design Patterns in C++ http://www.industriallogic.com/patterns/ili_nyc_examp les.html Eample of Design Patterns in C++ http://www.industriallogic.com/patterns/ili_nyc_examp les.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.