1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 Software Design State Pattern.

Slides:



Advertisements
Similar presentations
Introduction to Object Orientation System Analysis and Design
Advertisements

Object-Oriented Design
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 4 Slide 1 Slide 1 Use Case Diagram Corrected to match newer conventions.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Refactoring: Improving the Design of Existing Code © Martin Fowler, Martin Fowler.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Matt Klein 7/6/2009.  Behavioral Pattern  Intent  Allow an object to alter its behavior when its internal state changes. The object will appear to.
Chapter 12 Information Systems Chapter Goals Define the role of general information systems Explain how spreadsheets are organized Create spreadsheets.
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Object-Oriented Design
Chapter 13: Object-Oriented Programming
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
Abstract Classes and Interfaces
Software Design & Development Software Design & Development Programming Types Event Driven Programming Event driven programming Is a type of programming.
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Design Patterns.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
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.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
Refactoring: Improving the Design of Existing Code © Martin Fowler, Martin Fowler.
Object-Oriented Design
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Refactoring II Dealing with Polymorphism. Switch in Rental Switches on Movie! class Rental … public double getCharge() { double result = 0; switch (getMovie().getPriceCode()){
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
Object Oriented Programming
Design Patterns.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
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.
Refactoring1 Improving the structure of existing code.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Be “GUI ready” developing in RPG by Robert Arce from PrismaTech. Be “GUI ready” developing in RPG-ILE Presented by: Robert Arce.
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.
CSCE 240 – Intro to Software Engineering Lecture 3.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Data Base Design Steps 1.Define Entity Classes (Relations/Tables) and Keys. 2.Define Relationships between Entity Classes. Normalization Steps Eliminate.
Refactoring (1). Software Evolution Cope with change Feature bloat Design decay Code duplications “Pattern time is refactoring time” Make future changes.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Design Patterns: MORE Examples
Feature of Online Billing Software
Design Skills Example.
Strategy: A Behavioral Design Pattern
Abstract Factory Pattern
Strategy Design Pattern
Module Road Map Refactoring Why Refactoring? Examples
CS 350 – Software Design The Strategy Pattern – Chapter 9
Chapter Nine The Strategy Pattern
Object-Oriented Database Management System (ODBMS)
Behavioral Design Patterns
Abstract Factory Pattern
Strategy Design Pattern
More Selections BIS1523 – Lecture 9.
State Design Pattern 1.
CSE3311 Software Design State Pattern.
DESIGN PATTERNS : State Pattern
Design pattern Lecture 9.
Extended Learning Module G
Functions, Procedures, and Abstraction
Refactoring.
Presentation transcript:

1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 Software Design State Pattern

2 Problem  You are developing a program to keep track of movies for a video store. There are three movie types: regular, children’s, and new releases.  The charge for a movie will depend on the length of the rental and its type. Also, customers receive a number of frequent renter points that depend on the type of the movie rented.  We need the ability to calculate the price and frequent renter points that accrue to a customer renting a movie. Develop a BON diagram demonstrating the classes you might include in this system along with their relationships. Do not be concerned with user interface classes.

3 First Design class MOVIE1 create make feature {NONE} – Initialization make(n,t: STRING) is do name := n type := t end feature name: STRING type: STRING

4 First Design price(days_rented: INTEGER): REAL is require positive_days: days_rented >= 0 do if type.is_equal("REG") then Result := 2 if days_rented > 2 then Result := Result + (days_rented - 2) * 1.5 end elseif type.is_equal("NEW") then Result := days_rented * 3 elseif type.is_equal("CHI") then Result := 1.5 if days_rented > 3 then Result := Result + (days_rented - 3) * 1.5 end

5 First Design points: INTEGER -- frequent renter points do if type.is_equal("REG") then Result := 100 elseif type.is_equal("NEW") then Result := 200 elseif type.is_equal("CHI") then Result := 150 end set_type(t: STRING) do type := t.twin end end -- class MOVIE1 How will we change the state from NEW to REG?

6 First Design test class make local m1, m2, m3: MOVIE1 do -- Design 1: It works but changes are very cumbersome print("Design1%N") create m1.make("Casablanca", "REG") create m2.make("Bambi", "CHI") create m3.make("The Lord of the Rings", "NEW") print (m1.price(2).out + "%N") print (m2.price(1).out + "%N") print (m3.price(5).out + "%N") print (m3.points.out + " points%N") m3.set_type ("REG") print("The new price for The Lord of the Rings is: ") print (m3.points.out + " points%N%N") end

7 Critique?  Suppose we need to add a new state ADVENTURE_MOVIE?  price is calculated differently  frequent renter points is calculated different  Will need to change multiple places (price and points routine logic)  there may be many more type dependent routines such as special_discount etc.  Single Choice Principle Violated

8 Design Two price* points* Change state (e.g.state ”reg”, “chi”) from class STRING to a class MOVIE2 Use polymorphism and dynamic binding to delegate calculation of price and points to MOVIE2 Adding a new state is easy Removes if … elseif … elseif … end statements in class MOVIE1

9 Second Design test class make is -- Creation procedure. local m1, m2, m3: MOVIE1 m4, m5, m6: MOVIE2 do -- Design 1: It works but changes are very cumbersome -- Design2: Takes advantage of polymorphism print("Design2%N") create {REGULAR} m4.make("Casablanca") create {CHILDRENS} m5.make("Bambi") create {NEW} m6.make("The Lord of the Rings") print (m4.price(2).out + "%N") print (m5.price(1).out + "%N") print (m6.price(5).out + "%N") print (m6.points.out + " points%N") print("How to change 'Lord of Rings' to regular???%N%N") end Position of Single Choice Delegate price & points to MOVIE2 (polymorphism + dynamic binding)

10 Final Design! price* points* price points set_type Delegate price & points to MOVIE_STATE Dynamic change of the state Single choice happens here

11 Final Design class MOVIE3 create make feature {NONE} – Initialization make(n,t: STRING) is do name := n; set_type(t) end feature name: STRING type: MOVIE_STATE set_type (t: STRING) do if t.is_equal("NEW") then create {NEW_STATE} type elseif t.is_equal("REG") then create {REGULAR_STATE} type elseif t.is_equal("CHI") then create {CHILDRENS_STATE} type end price (days_rented: INTEGER): REAL is do Result := type.price(days_rented) end points: INTEGER do Result := type.points end end Single Choice Delegate price & points to MOVIE_STATE

12 Final Design test class  Design 1: It works but changes are very cumbersome  Design2: Takes advantage of polymorphism, but what if "The Lord of the Rings" is no longer a NEW movie?  Design 3: Polymorphism is now delegated to the state object. Adding a new state is trivial make local m7, m8, m9: MOVIE3 do create m7.make("Casablanca", "REG") create m8.make("Bambi", "CHI") create m9.make("The Lord of the Rings", "NEW") print (m7.price(2).out + "%N") print (m8.price(1).out + "%N") print (m9.price(5).out + "%N") print (m9.points.out + " points%N") m9.set_type ("REG") print("The new price for The Lord of the Rings is: ") print (m9.price(5).out + "%N") print (m9.points.out + " points%N") end

13 State Pattern: allows an object to alter its behaviour when its internal state changes. The object will appear to change its class By encapulating each state we localize any changes that will need to be made (e.g. price and points) to that state (behaviour)

14

15

16 State vs. Strategy  The class diagrams are similar but they differ in intent  State  Behaviours are constantly changing over time and the client (context) knows very little about how those different behaviours work  Encapsulate behaviours in state objects and set change in the context  Alternative to putting a lot of conditionals in the context  Strategy  Client knows quite a lot about what behaviour (state) is most appropriate e.g. we know that a mallard duck has typical flying behaviour and a decoy duck never flies  Change in state less usual  Flexible alternative to subclassing

17

18

19 adding new states

20

21