Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.

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

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.
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
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
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 ( ) ( ) ( )
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.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Composite Design Pattern. Motivation – Dynamic Structure.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
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.
Case Studies on Design Patterns Design Refinements Examples.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
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.
 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?
Strategy Design Patterns CS 590L - Sushil Puradkar.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
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.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
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.
Stéphane Ducasse 1 Strategy.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
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.
The State Design Pattern A behavioral design pattern. Shivraj Persaud
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Design Patterns: MORE Examples
Mediator Design Pattern
Strategy: A Behavioral Design Pattern
Abstract Factory Pattern
Design Patterns: Brief Examples
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Chapter 10 Design Patterns.
Jim Fawcett CSE776 – Design Patterns Fall 2016
Design Patterns Lecture part 2.
Introduction to Design Patterns
Behavioral Design Patterns
Software Design and Architecture
Satisfying Open/Closed Principle
Flyweight Design Pattern
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Abstract Factory Pattern
Jim Fawcett CSE776 – Design Patterns Summer 2003
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Design pattern Lecture 9.
Strategy and Template Method Patterns, Single User Protection
Strategy Design Pattern
Strategy Design Pattern
Design Patterns (Gamma, Helm, Johnson, Vlissides)
Adapter Pattern Jim Fawcett
Adapter Pattern Jim Fawcett
Presentation transcript:

Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014

Motivation Code analysis requires parsing of code text that entails detection of specific language constructs and storing results. Placing the detection and storage in one class isn’t desirable for several reasons: Each grammatical construct needs a specific rule for detection and there are many such rules. The actions required after detection depend on which rule matches the current context. Actions may depend on the results of the actions of other rules.

Parser uses Strategy

Applicability Use the Strategy Pattern where: Many related classes differ only in their behavior (rules, actions) You need variants of an algorithm (rules, actions) An algorithm uses data a client shouldn’t know about (Token Collections) A class defines many behaviors and these appear as multiple conditional statements in its operations. Instead, move related conditional branches to their own Strategy class.

Structure

Participants Strategy (IRule, IAction) Declares interface common to all ConcreteStrategies Context uses this interface to call the algorithm defined by a ConcreteStrategy ConcreteStrategy (DetectClass, PushClass) Implements the algorithm using the Strategy interface Context (Parser for rules, Rule for actions, Repository) Is configured with Concrete Strategy objects (rules, actions) Maintains references to Strategy objects (Builder maintains references) May define an interface that lets Strategy access its data (Repository)

Collaborations

Collaborations Strategy (Rule, Action) and Context (Parser) interact to implement the chosen algorithm. A Context (Parser) may pass all data required by the algorithm (TokenCollection) to the Strategy (Rule) when the algorithm is called. Alternately the Context may pass a reference to itself to the Strategy. That lets the Strategy call back on the Context as required. A Context forwards requests from its clients to its Strategy. Clients usually create and pass a ConcreteStrategy object to the Context (Builder); Thereafter clients interact exclusively with the Context.

Consequences The Strategy Pattern has the following benefits and drawbacks: Hierarchies of Strategy classes define a family of algorithms or behaviors for Contexts to reuse. Inheritance can help to factor out common functionality of the algorithms. Encapsulating the algorithm in separate Strategy classes lets you vary the algorithm independently of its Context, making it easier to switch, understand, and extend. Strategies eliminate large sets of conditional statements. Clients must be aware of different Strategies (can use Builder to mitigate) Communication overhead between Strategy and Context Increased number of objects

Implementation The Strategy and Context interfaces must give a ConcreteStrategy efficient access to data it needs from the Context. Strategies as template parameters Templates can be used to configure a class with a Strategy. This makes sense when only one Strategy, selected by the client, is used for the duration of the processing of an algorithm. Sorting is one example.

Code Examples Flight Strategy StrategyPatternCode Sorting Strategy

Know Uses Parsers used in CSE681 – SMA and CSE687 – OOD Control systems where the processing for an operation depends on the operation’s context, e.g., flight navigation control. Games where player’s may choose one or more strategies to achieve some objective. Software tools like Code Analyzers that use parsers. Graphical processors that build parse trees, e.g., browser and its HTML parse tree

Related Patterns Decorator Template Method Composite Both Decorator and Strategy can dynamically add new behaviors Template Method Template method provides algorithm steps defined by a template Composite Both Composite and Strategy build processing out of predefined parts

End of Presentation