13
Strategy Strategy Design Patterns Design Patterns CS 590L CS 590L - Sushil Puradkar - Sushil Puradkar

Strategy Design Patterns CS 590L - Sushil Puradkar

Embed Size (px)

Citation preview

Page 1: Strategy Design Patterns CS 590L - Sushil Puradkar

StrategyStrategyDesign PatternsDesign Patterns

CS 590LCS 590L

- Sushil Puradkar- Sushil Puradkar

Page 2: Strategy Design Patterns CS 590L - Sushil Puradkar

IntentIntent

Define a family of algorithms, encapsulate each Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy one, and make them interchangeable. Strategy lets the algorithm vary independently from clients lets the algorithm vary independently from clients that use it.that use it.

Behavioral patternBehavioral pattern

AKA: PolicyAKA: Policy

Page 3: Strategy Design Patterns CS 590L - Sushil Puradkar

MotivationMotivation

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.

Page 4: Strategy Design Patterns CS 590L - Sushil Puradkar

ApplicabilityApplicability

Many related classes differ only in their Many related classes differ only in their behavior.behavior.

You need different variants of an algorithm. You need different variants of an algorithm. Strategy can be used as a class hierarchy of Strategy can be used as a class hierarchy of algorithms.algorithms.

An algorithm use data structures that clients An algorithm use data structures that clients shouldn’t know about.shouldn’t know about.

A class defines many behaviors, and these A class defines many behaviors, and these appear as multiple conditionals in its appear as multiple conditionals in its operation.operation.

Page 5: Strategy Design Patterns CS 590L - Sushil Puradkar

ExampleExample

Modes of transportation to an airport is an example of a Strategy. Several 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 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 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 transportation will get a traveler to the airport, and they can be used interchangeably. The traveler must chose the Strategy based on interchangeably. The traveler must chose the Strategy based on tradeoffs between cost, convenience, and time. tradeoffs between cost, convenience, and time.

Page 6: Strategy Design Patterns CS 590L - Sushil Puradkar

ExampleExample

Many algorithms exists for breaking a string into Many algorithms exists for breaking a string into lines.lines.

Simple Compositor is a simple linebreaking Simple Compositor is a simple linebreaking method.method.

TeX Compositor uses the TeX linebreaking TeX Compositor uses the TeX linebreaking strategy that tries to optimize linebreaking by strategy that tries to optimize linebreaking by breaking one paragraph at a time.breaking one paragraph at a time.

Array Compositor breaks a fixed number of Array Compositor breaks a fixed number of items into each row.items into each row.

Page 7: Strategy Design Patterns CS 590L - Sushil Puradkar

StructureStructure

Page 8: Strategy Design Patterns CS 590L - Sushil Puradkar

ParticipantsParticipants

Strategy Strategy declares an interface common to all supported declares an interface common to all supported algorithms. Context uses its interface to call the algorithms. Context uses its interface to call the algorithm defined by a ConcreteStrategy. algorithm defined by a ConcreteStrategy.

ConcreteStrategy ConcreteStrategy implements a specific algorithm using the Strategy implements a specific algorithm using the Strategy interface. interface.

Context Context – is configured with a ConcreteStrategy object. is configured with a ConcreteStrategy object. – maintains a reference to a Strategy object. maintains a reference to a Strategy object. – may define an interface for Strategy to use to may define an interface for Strategy to use to

access its access its

Page 9: Strategy Design Patterns CS 590L - Sushil Puradkar

ConsequencesConsequences Families of related algorithms Families of related algorithms

– Hierarchies of Strategy factor out common functionality of a family of Hierarchies of Strategy factor out common functionality of a family of algorithms for contexts to reuse. algorithms for contexts to reuse.

An alternative to subclassing An alternative to subclassing – Subclassing a Context class directly hard-wires the behavior into Subclassing a Context class directly hard-wires the behavior into

Context, making Context harder to understand, maintain, and extend. Context, making Context harder to understand, maintain, and extend. – Encapsulating the behavior in separate Strategy classes lets you vary Encapsulating the behavior in separate Strategy classes lets you vary

the behavior independently from its context, making it easier to the behavior independently from its context, making it easier to

understand, replace, and extendunderstand, replace, and extend. . Strategies eliminate conditional statements. Strategies eliminate conditional statements.

– Encapsulating the behavior into separate Strategy classes eliminates Encapsulating the behavior into separate Strategy classes eliminates

conditional statements for selecting desired behaviorconditional statements for selecting desired behavior. . A choice of implementations A choice of implementations

– Strategies can provide different implementations of the same Strategies can provide different implementations of the same behavior with different time and space trade-offs. behavior with different time and space trade-offs.

Page 10: Strategy Design Patterns CS 590L - Sushil Puradkar

Consequences (cont..)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 A client must understand how Strategies differ before it can select the appropriate one. the appropriate one.

– You should use the Strategy pattern only when the variation in You should use the Strategy pattern only when the variation in behavior is relevant to clients. 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. The Strategy interface is shared by all ConcreteStrategy classes. – It's likely that some ConcreteStrategies will not use all the It's likely that some ConcreteStrategies will not use all the

information passed to them through this common interface. information passed to them through this common interface. – To avoid passing data that get never used, you'll need tighter To avoid passing data that get never used, you'll need tighter

coupling between Strategy and Context. coupling between Strategy and Context. Increased number of objects Increased number of objects

– Strategies increase the number of objects in an application. Strategies increase the number of objects in an application. – Sometimes you can reduce this overhead by implementing Sometimes you can reduce this overhead by implementing

strategies as stateless objects that context can share. strategies as stateless objects that context can share. – The Flyweight pattern describes this approach in more detail. The Flyweight pattern describes this approach in more detail.

Page 11: Strategy Design Patterns CS 590L - Sushil Puradkar

Known UsesKnown Uses

EventListeners in JavaEventListeners in Java Layout managers in JavaLayout managers in Java Calculating financial instrumentsCalculating financial instruments

Page 12: Strategy Design Patterns CS 590L - Sushil Puradkar

Relater PatternRelater Pattern

Flyweight: Strategy objects often make good flyweights.Flyweight: Strategy objects often make good flyweights.

IntentIntent

Use sharing to support large number of fine-grained objects Use sharing to support large number of fine-grained objects efficiently.efficiently.

Page 13: Strategy Design Patterns CS 590L - Sushil Puradkar

ReferencesReferences

New Even Handling in Java New Even Handling in Java http://www.nada.kth.se/~li/Dais/L7.pdf

Examples of Design Patterns Examples of Design Patterns http://www.agcs.com/supportv2/techpapers/patterns/http://www.agcs.com/supportv2/techpapers/patterns/papers/patexamples.htmpapers/patexamples.htm

http://selab.korea.ac.kr/selab/courses/GoF-patternshttp://selab.korea.ac.kr/selab/courses/GoF-patterns Eample of Design Patterns in C++ Eample of Design Patterns in C++

http://www.industriallogic.com/patterns/ili_nyc_examphttp://www.industriallogic.com/patterns/ili_nyc_examples.htmlles.html