Fall 2005CSE 115/503 Introduction to Computer Science I1 Strategy Pattern Lab 5 retrospective –Base fish provided –Specialized fish were modeled as subclasses.

Slides:



Advertisements
Similar presentations
1.8 Combinations of Functions: Composite Functions The composition of the functions f and g is “f composed by g of x equals f of g of x”
Advertisements

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
Matt Klein 7/2/2009.  Intent  Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Computer Science – Game DesignUC Santa Cruz Today Publish/Subscribe Design Pattern Delegates Sprite movement Particles.
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Association relationship We’ve seen one implementation of “knows a”: public class Dog.
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Things to mention public static void main(String [] args) imports comments –block comments /* … */ –single-line comments // –javadoc comments and tags.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Reminder Check the course web site on a regular basis:
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Delegates & Events Observer and Strategy Patterns Game Design Experience Professor Jim Whitehead January 30, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Lecture #4 Agenda Announcements Review Questions? Classes and objects UML class diagrams Creating.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
The Binomial Theorem.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Introduction to Object-Oriented Programming
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
CPS120 Introduction to Computer Science Iteration (Looping)
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Working with arrays (we will use an array of double as example)
GoF Sections Design Problems and Design Patterns.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Counting Techniques and Some Other Math Team Strategies Susan Schwartz Wildstrom Walt Whitman High School Bethesda, MD NCTM National Meeting April 6, 2001.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
Section 2.3 Properties of Solution Sets
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.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
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.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
CS 5150 Software Engineering Lecture 16 Program Design 3.
The Trouble with Observer and Visitor Revisited PH Chapter 3 (pp ) Crystal Miller 03/22/06.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Today’s lecture Review chapter 5 go over exercises.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Software Design Refinement Using Design Patterns
Behavioral Design Patterns
Inheritance and Run time Polymorphism
The Binomial Theorem Objectives: Evaluate a Binomial Coefficient
Pascal’s Triangle MDM 4U Lesson 5.1.
Introduction to Computer Programming
SE-2811 Software Component Design
Examining Variables on Flow Paths
The Binomial Theorem OBJECTIVES: Evaluate a Binomial Coefficient
Properties of Solution Sets
Combinations of Functions
CIS 110: Introduction to computer programming
Computer Science II for Majors
Presentation transcript:

Fall 2005CSE 115/503 Introduction to Computer Science I1 Strategy Pattern Lab 5 retrospective –Base fish provided –Specialized fish were modeled as subclasses of the base fish –This works!

Fall 2005CSE 115/503 Introduction to Computer Science I2 Combinations of behaviors How is a fish with a combination of behaviors handled? Not so well…

Fall 2005CSE 115/503 Introduction to Computer Science I3 Two behaviors Can subclass an existing fish. Suppose we want a dizzy chameleon fish: with single-inheritance, our new fish can derive only from one existing fish. Which one?

Fall 2005CSE 115/503 Introduction to Computer Science I4 Two choices

Fall 2005CSE 115/503 Introduction to Computer Science I5 Which one? Either choice is equally bad: we must duplicate code of other’s update method to get both behaviors. public void update() { super.update(); setRotation(getRotation() + _rotationSpeed); } public void update() { super.update(); setColor(Utilities.randomColor()); }

Fall 2005CSE 115/503 Introduction to Computer Science I6 Combinations of more than two? Suppose we have N total individual behaviors (like Chameleon, Dizzy, etc.) Consider the basic fish to have a null behavior. There is one of these. There are N immediate subclasses of Fish, that have one added behavior.

Fall 2005CSE 115/503 Introduction to Computer Science I7 1 (non-null) behavior Total number of fish types is 2: 1 (with a null behavior) 1 (with a non-null behavior)

Fall 2005CSE 115/503 Introduction to Computer Science I8 2 (non-null) behaviors Total number of fish types is 4: 1 (with a null behavior) 2 (with a non-null behavior) 1 (with both behaviors)

Fall 2005CSE 115/503 Introduction to Computer Science I9 3 (non-null) behaviors Total number of fish types is 8: 1 (with a null behavior) 3 (with a non-null behavior) 3 (with a pair of behaviors) 1 (with all three behaviors)

Fall 2005CSE 115/503 Introduction to Computer Science I10 Another view of data …

Fall 2005CSE 115/503 Introduction to Computer Science I11 Pascal’s triangle … N Sum =2 N

Fall 2005CSE 115/503 Introduction to Computer Science I12 So how bad is this approach? The sum represents the number of distinct classes you need to define to accommodate all possible combinations of behaviors that are possible. Imagine adding one more behavior to the system: –this doubles the number of classes that need to be defined (e.g. from 32 to 64, or from 64 to 128) –requires compilation of all these classes –all possible combinations must be formed at compile time (statically), whether they’re needed or not This quickly becomes intractable!

Fall 2005CSE 115/503 Introduction to Computer Science I13 Strategy pattern What’s the basic problem? We’re stuck because behaviors are expressed as a few lines of code inside the class definition of a particular type of fish. These concepts are too tightly coupled. Why not model a behavior using an object? This lets us decouple behavior specification from fish specification. This is the basic insight of the Strategy Pattern.

Fall 2005CSE 115/503 Introduction to Computer Science I14 Strategy Pattern Fish specification and behavior specifications are decoupled.

Fall 2005CSE 115/503 Introduction to Computer Science I15 Combining behaviors One way is to make use of the Composite Pattern: Composite lets us combine behaviors dynamically!

Fall 2005CSE 115/503 Introduction to Computer Science I16 Consequences Behaviors are defined once, independently of fish. For N behaviors, only N+1 classes must be defined (one for each behavior, one for the composite). Combinations of behaviors are not determined statically. Only those combinations actually needed at runtime are formed, dynamically.