CS 210 Introduction to Design Patterns August 29, 2006.

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

+ Informatics 122 Software Design II Lecture 7 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Open-closed principle.
Chapter 4: The Factory Pattern. Consider the Following Code Fragment Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new.
The Strategy Pattern CSC 211: Design Patterns. Reading Quiz.
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?
Inheritance issues SE-2811 Dr. Mark L. Hornick 1.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Informatics 122 Software Design II Lecture 5 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
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.
COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided.
Design Patterns.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
1 Unit 5 Design Patterns: Design by Abstraction. 2 What Are Design Patterns?  Design patterns are: Schematic descriptions of design solutions to recurring.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
CS 210 Introduction to Design Patterns September 7 th, 2006.
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.
DAAD project “Joint Course on OOP using Java” On Object Oriented modeling in Java (Why & How) Ana Madevska Bogdanova Institute of informatics Faculty of.
Strategy Design Patterns CS 590L - Sushil Puradkar.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
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.
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 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
An Introduction To Design Patterns Jean-Paul S. Boodhoo Independent Consultant
The Strategy Design Pattern © Allan C. Milne v
CS 210 Review October 3, 2006.
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:
Systems Requirements SE 3821 Design? Algorithms CS 2852.
CS 210 Introduction to OO Design August 24, 2006
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
IN 076 Pola Desain Perangkat Lunak Dosen: Hapnes Toba Oscar Karnalim
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.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
CS 210 Introduction to Design Patterns September 14 th, 2006.
SE 461 Software Patterns Welcome to Design Patterns.
Intro to Design Pattern
Strategy: A Behavioral Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Strategy Pattern.
Introduction to Design Patterns
CS 350 – Software Design The Strategy Pattern – Chapter 9
Chapter Nine The Strategy Pattern
Behavioral Design Patterns
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Strategy Design Pattern
SE-2811 Software Component Design
State Design Pattern 1.
Object-Oriented Programming
SE-2811 Software Component Design
Design pattern Lecture 9.
Strategy Design Pattern
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Delegation vs inheritance
Design Patterns Lecture part 1.
Software Design Lecture : 27.
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

CS 210 Introduction to Design Patterns August 29, 2006

Head First: Design Patterns Eric Freeman & Elisabeth Freeman O’Reilly Press, 2004

Introduction to Design Patterns Chapter 1 Strategy Pattern

Goals for this week Learn how to exploit and gain experience of others Examine the benefits of design pattern Learn one specific pattern: Strategy pattern

Simple Simulation of Duck behavior Duck quack() swim() display() // other duck methods MallardDuck display() // looks like mallard RedheadDuck display() // looks like redhead Other duck types

What if we want to simulate flying ducks? Duck quack() swim() display() fly() // other duck methods MallardDuck display() // looks like mallard RedheadDuck display() // looks like redhead Other duck types

Tradeoffs in use of inheritance and maintenance Duck quack() swim() display() fly() // other duck methods MallardDuck display() // looks like mallard RubberDuck quack() //overridden to squeak display() // looks like rubberduck fly() // override to do nothing RedheadDuck display() // looks like redhead One could override the fly method to the appropriate thing – just as the quack method below.

Example complicated: add a wooden decoy ducks to the mix DecoyDuck quack(){ // override to do nothing } display() // display decoy duck fly (){ //override to do nothing } Inheritance is not always the right answer. Every new class that inherits unwanted behavior needs to be overridden. How about using interfaces instead?

Duck simulation recast using interfaces. Duck swim() display() // other duck methods MallardDuck display() fly() quack() Quackable quack() Flyable fly() RedheadDuck display() fly() quack() RubberDuck display() quack() DecoyDuck display() Interfaces

Pros and cons Not all inherited methods make sense for all subclasses – hence inheritance is not the right answer But by defining interfaces, every class that needs to support that interface needs to implement that functionality… destroys code reuse! So if you want to change the behavior defined by interfaces, every class that implements that behavior may potentially be impacted And….

Design Principle Identify the aspects of your application that vary and separate them from what stays the same. OR 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.

In the Duck simulation context… Duck Behaviors Parts that vary Parts that stay the same

Design Principle Program to an interface, not to an implementation. Really means program to a super type.

Program to an interface Programming to an implementation Dog d = new Dog(); d.bark(); Programming to an interface Animal animal = new Dog(); animal.makesound();

Implementing duck behaviors - revisited FlyWithWings fly(){ // implements duck flying } > FlyBehavior fly() > QuackBehavior quack() Quack quack(){ // implements duck quacking } FlyNoWay fly(){ // do nothing – Can’t fly } Squeak quack(){ // implements duck squeak } MuteQuack quack(){ // do nothing – Can’t quack }

Integrating the duck behavior Key now is that Duck class will delegate its flying and quacking behavior instead of implementing these itself.

In the Duck simulation context… Duck Behaviors Duck FlyBehavior: flyBehavior QuackBehavior: quackBehavior performQuack() swim() display() performFly() //other duck-like methods

Duck simulation recast using the new approach MallardDuck display() RedHeadDuck display() RubberDuck display() DecoyDuck display() Duck FlyBehavior: flyBehavior QuackBehavior: quackBehavior performQuack() performFly() setFlyBehavior() setQuackBehavior() swim() display() > FlyBehavior fly() FlyWithWings fly() // implements duck flying FlyNoWay fly() // do nothing – Can’t fly > QuackBehavior quack() Quack quack() // implements duck quacking Squeak quack() // implements squeak Mutequack quack() // do nothing

Design Principle Favor composition over inheritance HAS-A can be better than IS-A Allows changing behavior at run time

The strategy pattern The 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.

Rationale for design patterns Shared pattern vocabularies are powerful Patterns allow you to say more with less Reusing tried and tested methods Focus is on developing flexible, maintainable programs