Stéphane Ducasse 1 Strategy.

Slides:



Advertisements
Similar presentations
MicroKernel Pattern Presented by Sahibzada Sami ud din Kashif Khurshid.
Advertisements

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.
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.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
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.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Composite Design Pattern. Motivation – Dynamic Structure.
Design Patterns Discussion of pages: xi-11 Sections: Preface, Forward, Chapter
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
S.Ducasse Stéphane Ducasse 1 Selected Design Patterns.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
MVC and MVP. References enter.html enter.html
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.
“A labor of love” (Multicast Pattern) Chapter 4 (pages or ) Chris Gordon.
CS 4240: More Design Patterns Readings: Chap. 9. Let’s Recap Some Ideas and Strategies We’ll assume some design-planning is useful up-front Design with.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Strategy Design Patterns CS 590L - Sushil Puradkar.
GRASP: Designing Objects with Responsibilities
Software Design 8.1 Model, View, Controller: battleship l Who does what, where are responsibilities in MVC?  This is a pattern, so there's isn't "one.
GoF: Document Editor Example Rebecca Miller-Webster.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Stéphane Ducasse 1 Elements of Design - Inheritance/Compositio n.
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.
Stéphane Ducasse 1 Singleton.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
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.
Pattern Bridge. Definition Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
S.Ducasse Stéphane Ducasse 1 Decorator.
S.Ducasse Stéphane Ducasse 1 Adapter.
STRATEGY PATTERN By Michelle Johnson. BACKGROUND Behavioral Pattern Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable.
The State Design Pattern A behavioral design pattern. Shivraj Persaud
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Strategy: A Behavioral Design Pattern
Design Patterns: Brief Examples
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Strategy Pattern.
Design Patterns Lecture part 2.
Software Design and Architecture
Presented by Igor Ivković
Design Patterns A Case Study: Designing a Document Editor
Jim Fawcett CSE776 – Design Patterns Summer 2003
State Design Pattern 1.
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Design pattern Lecture 9.
Strategy and Template Method Patterns, Single User Protection
Structural Patterns: Adapter and Bridge
Strategy Design Pattern
Design Patterns (Gamma, Helm, Johnson, Vlissides)
Presented by Igor Ivković
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

Stéphane Ducasse 1 Strategy

S.Ducasse 2 Strategy Define a family of algorithms, encapsulate each in a separate class and define each class with the same interface so that they can be interchangeable. Also Know as Policy

S.Ducasse 3 Strategy Intent Define a family of algorithms, encapsulate each in a separate class and define each class with the same interface so that they can be interchangeable.

S.Ducasse 4 Motivation Many algorithms exist for breaking a stream into lines. Hardwiring them into the classes that requires them has the following problems: Clients get more complex Different algorithms can be used at different times Difficult to add new algorithms at run-time

S.Ducasse 5 Code Smells Composition>>repair formatting == #Simple ifTrue: [ self formatWihtSimpleAlgo] ifFalse: [ formatting == #Tex ifTrue: [self formatWithTex]....]

S.Ducasse 6 Alternative Composition>>repair | selector | selector := (‘formatWith, formatting) asSymbol. self perform: selector Still your class gets complex...

S.Ducasse 7 Inheritance? May not be the solution since: - you have to create objects of the right class - it is difficult to change the policy at run-time - you can get an explosion of classes bloated with the use of a functionality and the functionalities. - no clear identification of responsibility

S.Ducasse 8 Strategy Solution

S.Ducasse 9 When Many related classes differ only in their behavior You have variants of an algorithm (space/time) An algorithm uses data that the clients does not have to know

S.Ducasse 10 Structure Composition>>repair formatter format: self

S.Ducasse 11 Participants Strategy (Compositor) declares an interface common to all concrete strategies Concrete Strategies implement algorithm Context configure with concrete strategy maintains a reference to the concrete strategy may define an interface to let the strategy access data

S.Ducasse 12 Collaborations (i) Strategy and Context interact to implement the chosen algorithm. A context may pass all data required by the algorithm to the strategy when the algorithm is called GraphVisualizer>>graphIt.... grapher plot: data using: graphPane pen Grapher>>plot: data using: aPen

S.Ducasse 13 Context passes itself as argument Also know as self-delegation... GraphVisualizer>>graphIt grapher plotFor: self BartChartGrapher>>plotFor: aGraphVisualizer |data| data := aGraphVisualizer data....

S.Ducasse 14 BackPointer Grapher class>>for: aGraphVisualizer ^ self new graphVisualizer: aGraphVisualizer BartChartGrapher>>plot... graphVisualizer data.. graphVisualizer pen Grapher (Strategy) points directly to GraphVisualizer (Context), so sharing strategy between different context may be difficult, if sharing is needed then use self-delegation

S.Ducasse 15 Collaboration (ii) “A context forwards requests from its clients to its strategy. Clients usually create and pass a ConcreteStrategy object to the context; thereafter, clients interact with the context exclusively. “ GOF Not sure that the client has to choose...

S.Ducasse 16 Consequences Define a family of pluggable algorithms Eliminates conditional statements Clients can choose between several implementations Clients must be aware of the different strategies Increase the number of objects Communication overhead between client and strategies Weaken encapsulation of the client

S.Ducasse 17 Domain-Specific Objects as Strategies Strategies do not have to be limited to one single algorithm They may represent domain specific knowledge Mortgage FixedRateMortgage OneYear...

S.Ducasse 18 Known Uses ImageRenderer in VW: “a technique to render an image using a limited palette” ImageRenderer NearestPaint OrderedDither ErrorDiffusion View-Controller a view instance uses a controller object to handle and respond to user input via mouse or keyboard. Controllers can be changed at run- time