OO Design Patterns - Decorator

Slides:



Advertisements
Similar presentations
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
Advertisements

Chapter 3: The Decorator Pattern
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
CS 210 Introduction to Design Patterns September 12 th, 2006.
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.
Marcelo Santos 1 Design Patterns Object Oriented Programming Advanced Course 2007.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
CS 210 Introduction to Design Patterns September 28 th, 2006.
9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
Lecture 16 Composition vs Inheritance: The Final Chapter.
CS 210 Introduction to Design Patterns September 7 th, 2006.
Jan Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
Decorator Pattern So many options!. Starbuzz Coffee  Want to offer a variety of combinations of coffee and condiments  Cost of a cup depends on the.
CSC 313 – Advanced Programming Topics. Open-Closed Principle Classes should be open for extension, but closed to modification  So, what does this mean?
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
Design Patterns. OO-Concepts Don’t rewrite code Encapsulation Inheritance Write flexible code.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
CS 210 Final Review November 28, CS 210 Adapter Pattern.
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Design Patterns: Elements of Reusable Object- Orientated Software Gamma, Helm, Johnson, Vlissides Presented By: David Williams.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Five design principles
Principles of Object Oriented Design
CS 210 Review October 3, 2006.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Week 5, Day 2: Decorator Decorators Muddiest Point Tomorrow: Quiz on lab reading: web.msoe.edu/hasker/se2811/labs/5/ SE-2811 Slide design:
Object- oriented Design Principles
CS, AUHenrik Bærbak Christensen1 Compositional Design Principles The “GoF” principles Or Principles of Flexible Design.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
The Decorator Pattern Decorators in Java I/O classes SE-2811 Dr. Mark L. Hornick 1.
Builder Introduction. Intent Separate the construction of a complex object from its representation so that the same construction process can create different.
CS 210 Introduction to Design Patterns September 14 th, 2006.
Design Patterns: MORE Examples
Object-Orientated Analysis, Design and Programming
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Compositional Design Principles
object oriented Principles of software design
Factory Method, Abstract Factory, and More
Strategy Design Pattern
OO Design - Observer Pattern
7. Decorator, Façade Patterns
Decorator Pattern.
lecture 08, OO Design Principle
Object Oriented Design Patterns - Structural Patterns
Decorator Pattern Richard Gesick.
Object-Oriented Programming
Week 7, Class 1: The Command Pattern (cont.)
CS 350 – Software Design Principles and Strategies – Chapter 14
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
7. Decorator SE2811 Software Component Design
11. MVC SE2811 Software Component Design
Week 8, Class 3: Model-View-Controller
Slide design: Dr. Mark L. Hornick
11. MVC SE2811 Software Component Design
Object Oriented Design & Analysis
Presentation transcript:

OO Design Patterns - Decorator Object-Oriented Programming

Starbuzz Coffee current design Đại học Công nghệ - ĐHQG HN OO Design Pattern

Starbuzz Coffee problem In addition to coffee, customers can ask for condiments like milk, mocha, soy,… then topped with whipped cream How to modify the current system to allow that? Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN Inheritance

Better, but there’re still potential issues What requirements or other factors might change that will impact this design? Price changes for condiments New condiments New beverages What about a double mocha? And ??? Đại học Công nghệ - ĐHQG HN Inheritance

Open-Closed Principle Open to extension: you can create subclasses Closed to modification: don’t modify existing code But be careful! Apply OCP principle EVERYWHERE is wasteful, unnecessary, and can lead to complex, hard to understand code Đại học Công nghệ - ĐHQG HN OO Design Pattern

Decorator Pattern A customer wants a Dark Roast with Mocha and Whip, we’ll: Take a DarkRoast object Decorate it with a Mocha object Decorate it with a Whip object Call the cost() method and rely on delegation to add on the condiment costs. Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern

Decorating our beverages Đại học Công nghệ - ĐHQG HN OO Design Pattern

Question Why should Beverage be an abstract class instead of an interface? Đại học Công nghệ - ĐHQG HN OO Design Pattern

Question Why should Beverage be an abstract class instead of an interface? Answer: Interface would work just as fine, but the existing code is an abstract class. Don’t alter existing code if it works fine. Đại học Công nghệ - ĐHQG HN OO Design Pattern

Question How to make a double mocha, soy latte with whip? Đại học Công nghệ - ĐHQG HN OO Design Pattern

Realworld Decorator: Java I/O Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern

OO Design Principles We’ve learned so far Encapsulate what varies Favor composition over inheritance Program to interfaces, not implementations Strive for loosely coupled designs between objects that interact. Open for extension, closed for modification Class is about behavior And more…. in the textbook. Đại học Công nghệ - ĐHQG HN OO Design Pattern