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.

Slides:



Advertisements
Similar presentations
+ Informatics 122 Software Design II Lecture 7 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Advertisements

Winter 2007ACS-3913 Ron McFadyen1 Also known as publish/subscribe The essence of this pattern is that one or more objects (called observers or listeners)
Chapter 4: The Factory Pattern. Consider the Following Code Fragment Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new.
D ESIGN P ATTERNS Introduction. C OURSE D ESCRIPTION Traditionally, OO designers have developed their own private "catalogs" of solutions to recurring.
Informatics 122 Software Design II
Strategy Pattern1 Design Patterns 1.Strategy Pattern How to design for flexibility?
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Name of.
March Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
Fall 2009ACS-3913 Ron McFadyen Composite Pattern Problem: How do we treat a composition structure of objects the same way as a non-composite object? Arises.
Informatics 122 Software Design II Lecture 5 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Spring 2010ACS-3913 Ron McFadyen1 Weather Station Page 39+ In this application, weather station devices supply data to a weather data object. As the data.
Fall 2009ACS Ron McFadyen1 The context maintains an instance of a concrete state subclass State Pattern Each subclass (concrete state) implements.
Spring 2010ACS-3913 Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Useful for.
Winter 2007ACS-3913 Ron McFadyen1 Observer Pattern Problem: There are many objects (observers / subscribers) needing to know of the state changes, or events,
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Winter 2011ACS-3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
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.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
CS 210 Introduction to Design Patterns September 28 th, 2006.
1 Unit 5 Design Patterns: Design by Abstraction. 2 What Are Design Patterns?  Design patterns are: Schematic descriptions of design solutions to recurring.
 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.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
GoF: Document Editor Example Rebecca Miller-Webster.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
November Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent groups,
2007ACS-3913 Ron McFadyen1 Class Diagram See Schaum’s UML Outline, especially chapters 4, 5, 6, 7.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
SE-2811 Software Component Design Week 1, Day 2 (and 1-3 and 2-1) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.3 Advanced Class Design –2.3.4 Design Patterns –2.3.5 Singleton Pattern –2.3.6.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Introduction
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
CS 210 Introduction to Design Patterns August 29, 2006.
The Strategy Design Pattern © Allan C. Milne v
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.
CS 210 Review October 3, 2006.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
It started with a simple … A highly successful duck pond simulation game called SimUDuck The game can show a large variety of duck swimming and making.
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.
STRATEGY PATTERN By Michelle Johnson. BACKGROUND Behavioral Pattern Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
SE 461 Software Patterns Welcome to Design Patterns.
Intro to Design Pattern
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Strategy Pattern.
Factory Patterns 1.
Behavioral Design Patterns
Strategy Design Pattern
Presented by Igor Ivković
SE-2811 Software Component Design
State Design Pattern 1.
Object-Oriented Programming
SE-2811 Software Component Design
DESIGN PATTERNS : Strategy Pattern
Design pattern Lecture 9.
Strategy Design Pattern
Design Patterns (Gamma, Helm, Johnson, Vlissides)
Presented by Igor Ivković
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

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 their own fly() and quack() operations Maintenance appears difficult as new duck types are required and as behaviour changes. As flying is seen to be subject to change there is motivation to extract that behaviour into its own class As quacking is seen to be subject to change there is motivation to extract that behaviour into its own class The above two points lead to the strategy pattern – once for flying and once for quacking.

Winter 2007ACS-3913 Ron McFadyen2 Consider that we have a family or collection of algorithms, one of which is to utilized at runtime by a client. When we utilize the strategy pattern we make the algorithms interchangeable and implement each in its own subclass of an interface or abstract class. At runtime the appropriate algorithm is associated with the client. Strategy Pattern

Winter 2007ACS-3913 Ron McFadyen3 Generic pattern Context … «interface» Strategy … ConcreteStrategy 1 … ConcreteStrategy n … … Note that the context knows only the strategy interface and not the concrete strategy types. This is all the context needs to know and this allows us to add/remove strategies without affecting the context.

Winter 2007ACS-3913 Ron McFadyen4 Generic pattern with collaboration Context … «interface» Strategy … ConcreteStrategy 1 … ConcreteStrategy n … … Strategy Concrete strategy strategy context The strategy participant declares the common interface for all supported algorithms. The context uses this interface to invoke an algorithm defined by a ConcreteStrategy. Each concrete strategy implements an algorithm using the strategy interface. The context participant is composed with a ConcreteStrategy object.

Winter 2007ACS-3913 Ron McFadyen5 Duck Example Duck display() performFly() setFlyBehavior() … «interface» FlyBehavior fly() MallardDuck display() … FlyWithWings fly() FlyNoWay fly() Quack quack() «interface» QuackBehavior quack() Squeak quack() Mute quack() flyBehavior quackBehavior DecoyDuck display() … … The algorithms that implement flying are each encapsulated in a subclass of FlyBehavior, and the algorithms that implement quacking are each encapsulated in a subclass of QuackBehavior. A duck is composed with one flying behaviour and one quacking behaviour.

Winter 2007ACS-3913 Ron McFadyen6 Duck Example Duck display() performFly() setFlyBehavior() … «interface» FlyBehavior fly() MallardDuck display() … FlyWithWings fly() FlyNoWay fly() Quack quack() «interface» QuackBehavior quack() Squeak quack() Mute quack() flyBehavior quackBehavior DecoyDuck display() … … Modify this class diagram to include the strategy collaborations. (There are two.)

Winter 2007ACS-3913 Ron McFadyen7 Duck Example Consider the text example. Examine the code to ensure you understand how the strategy pattern is implemented. Run the example. Is strategy applied once or twice? Three design principles are discussed. Identify the aspects of your application that vary and separate them from what stays the same Program to an interface and not an implementation Favour composition over inheritance

Winter 2007ACS-3913 Ron McFadyen8 Duck Example Suppose you are a developer and there is a requirement for a new quacking behavour. What must you change in the existing code? What classes must you write? Draw sequence diagrams to illustrate behaviour. What messages are sent When a mallard duck flies? When the flying behaviour for a mallard is changed from flying to walking? When the Main() on page 21 executes?