Strategy Design Patterns CS 590L - Sushil Puradkar.

Slides:



Advertisements
Similar presentations
GoF State Pattern Aaron Jacobs State(305) Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Advertisements

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use.
Winter 2007ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Strategy AKA Policy Dale Willey Ian Price. Overview Definitions Difference between Strategy pattern and strategy Where would you use this? Challenges.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Design Patterns I 1. Creational Pattern Singleton: intent and structure Ensure a class has one instance, and provide a global point of access to it 2.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
Case Studies on Design Patterns Design Refinements Examples.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
CSC 313 – Advanced Programming Topics. Design Pattern Intent  Each design pattern is a tool  Like all tools, have reason for being.
 How are you going to collaborate?  How are you going to divide up work?  How are you going to make sure that changes work with other people’s code?
CS 350 – Software Design The Strategy Pattern – Chapter 9 Changes to software, like other things in life, often focus on the immediate concerns and ignore.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
Structural Design Patterns
Behavioral Pattern: Strategy C h a p t e r 5 – P a g e 205 The Open-Closed Principle advocates designing software in such a way that it will absorb new.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
OO Methodology Elaboration Iteration 2 - Design Patterns -
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Pattern Bridge. Definition Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently.
Stéphane Ducasse 1 Strategy.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
STRATEGY PATTERN By Michelle Johnson. BACKGROUND Behavioral Pattern Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
By SmartBoard Team. Agenda  Scenario & Writing UML  Discuss with each team’s UML  What if…  BMVV design  Strategy pattern  Applied with BMVV design.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns: MORE Examples
Strategy: A Behavioral Design Pattern
Design Patterns: Brief Examples
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Strategy Pattern.
CS 350 – Software Design The Strategy Pattern – Chapter 9
Presented by Igor Ivković
State Design Pattern 1.
Object Oriented Design Patterns - Structural Patterns
DESIGN PATTERNS : Strategy Pattern
Design pattern Lecture 9.
Strategy and Template Method Patterns, Single User Protection
Strategy Design Pattern
Informatics 122 Software Design II
Design by Abstraction (Continuation) CS 3331 Spring 2005
Design Patterns (Gamma, Helm, Johnson, Vlissides)
Presented by Igor Ivković
Software Design Lecture : 28.
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

Strategy Design Patterns CS 590L - Sushil Puradkar

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

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.

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.

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.

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.

Structure

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

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.

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.

Known Uses EventListeners in Java EventListeners in Java Layout managers in Java Layout managers in Java Calculating financial instruments Calculating financial instruments

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.

References New Even Handling in Java New Even Handling in Java Examples of Design Patterns papers/patexamples.htm Examples of Design Patterns papers/patexamples.htm papers/patexamples.htm papers/patexamples.htm Eample of Design Patterns in C++ les.html Eample of Design Patterns in C++ les.html