Strategy Design Pattern

Slides:



Advertisements
Similar presentations
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.
Advertisements

Strategy Pattern1 Design Patterns 1.Strategy Pattern How to design for flexibility?
Inheritance Inheritance Reserved word protected Reserved word super
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.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
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.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented.
DAAD project “Joint Course on OOP using Java” On Object Oriented modeling in Java (Why & How) Ana Madevska Bogdanova Institute of informatics Faculty of.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
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.
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
Object Oriented Programming
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
CS 210 Introduction to Design Patterns August 29, 2006.
Coming up: Inheritance
The Strategy Design Pattern © Allan C. Milne v
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CS 210 Review October 3, 2006.
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.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
STRATEGY PATTERN By Michelle Johnson. BACKGROUND Behavioral Pattern Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable.
Object Oriented Programming Some Interesting Genes.
SE 461 Software Patterns Welcome to Design Patterns.
Intro to Design Pattern
Design Patterns: MORE Examples
Strategy Pattern.
Factory Patterns 1.
Introduction to Design Patterns
Inheritance and Polymorphism
CS 350 – Software Design The Strategy Pattern – Chapter 9
Behavioral Design Patterns
Week 4 Object-Oriented Programming (1): Inheritance
Types of Programming Languages
Section 11.1 Class Variables and Methods
object oriented Principles of software design
Interfaces and Inheritance
Presented by Igor Ivković
Object Oriented Practices
SE-2811 Software Component Design
“The Trouble with Observer” and “Visitor Revisited”
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Object-Oriented Programming
Advanced Java Programming
SE-2811 Software Component Design
Java Inheritance.
Strategy Design Pattern
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Delegation vs inheritance
Design Patterns Lecture part 1.
Chapter 14 Abstract Classes and Interfaces
Presented by Igor Ivković
Software Design Lecture : 27.
HFOOAD Chapter 5 Interlude
Presentation transcript:

Strategy Design Pattern COSC3311 – Software Design Strategy Design Pattern

(c) Head First Design Patterns chapter 1

Someone has already solved your problem Exploit the wisdom and lessons learned by other developers who’ve been down the same design problem road and survived the trip. Instead of code reuse, with patterns you get experience reuse.

Joe’s problem: SimUDuck

superclass DUCK

In the last year, the company has been under increasing pressure from competitors. After a week long off-site brainstorming session over golf, the company executives think it’s time for a big innovation. They need something really impressive to show at the upcoming shareholders meeting in Maui next week.

What do you think about this design? Java interface

We know that not all of the subclasses should have flying or quacking behavior, so inheritance isn’t the right answer. But a Java interface has no implementation – so we have now destroyed code reuse What about Eiffel? Not a problem because we have full support for multiple inheritance

Problem: Find a design? Most ducks fly in the same way, but a few species of duck have different flyable behaviour, or perhaps they do not fly at all Most ducks quack in the same way, but a few species have a different quackable behaviour or they do not quack at all Must be able to change flyable and quackable behavior dynamically

Okay, what’s the one thing you can always count on in software development? No matter where you work, what you’re building, or what language you are programming in, what’s the one true constant that will be with you always?

The one constant No matter how well you design an application, over time an application must grow and change or it will die.

Inheritance hasn’t worked out very well, since the duck behavior keeps changing across the subclasses, and it’s not appropriate for all subclasses to have those behaviors. The Flyable and Quackable interface sounded promising at first—only ducks that really do fly will be Flyable, etc.—except Java interfaces have no implementation code, so no code reuse. And that means that whenever you need to modify a behavior, you’re forced to track down and change it in all the different subclasses where that behavior is defined, probably introducing new bugs along the way!

Design Principle take the parts that vary and encapsulate them, so that later you can alter or extend the parts that vary without affecting those that don’t Identify the aspects of your application that vary and separate them from what stays the same.

We’d like to keep things flexible We’d like to keep things flexible. It was the inflexibility in the duck behaviors that got us into trouble in the first place. And we know that we want to assign behaviors to the instances of Duck. For example, we might want to instantiate a new MallardDuck instance and initialize it with a specific type of flying behavior. And while we’re there, why not make sure that we can change the behavior of a duck dynamically?

Design Principle Program to an interface not to an implementation Duck behaviours (e.g. flying, quacking) will live in a class that is separate from class DUCK. DUCK does not need to know how those behaviours are implemented

Not a Java “interface” The word “interface” is overloaded Here we do not mean a Java interface – rather we mean the concept of an interface Programming to an implementation list: LINKED_LIST[PERSON] Programming to an interface (supertype) list: LIST[PERSON] create {LINKED_LIST[PERSON]}list.make

Delegation

Has-a Is-a

Question? Should we make DUCK an <<interface>> as well? Not in this case. Duck can be abstract and some descendants like MALLARD_DUCK inherit common properties and methods. Now that we’ve removed what varies from the Duck inheritance, we get the benefits of this structure without the problems.

Question? Using our new design, what would you do if you needed to add rocket-powered flying to the SimUDuck app? class ROCKET_POWERED_DUCK inherit FLY_BEHAVIOUR

Program to an interface not to an implementation

MALLARD_DUCK?

Good catch, that’s exactly what we’re doing. for now Good catch, that’s exactly what we’re doing... for now. [Other creator patterns can help] Still, notice that while we are setting the behaviors to concrete classes Quack or FlyWithWings, but we are assigning it to our behavior reference variable, and that could easily change at runtime. How? [see in a few slides]

class DUCK

Setting behavior dynamically

Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. use this definition when you want impress friends & influence key executives

Patterns give developers a shared vocabulary as well as a shared code experience