SE-2811 Dr. Mark L. Hornick 1. The Decorator Pattern SE-2811 Dr. Mark L. Hornick 2.

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
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
CS 210 Introduction to Design Patterns September 12 th, 2006.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Prototype Pattern Intent:
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
Designing with Procedures 1. Designing a Program with Procedures If the code for your program is going to be less than one page, normally don’t bother;
Lecture 16 Composition vs Inheritance: The Final Chapter.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.
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.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Week 2, Day 2: The Factory Method Pattern Other good design principles Cohesion vs. Coupling Implementing the Strategy Pattern Changing strategies (behaviors)
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
Design Patterns. OO-Concepts Don’t rewrite code Encapsulation Inheritance Write flexible code.
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Introduction
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
S.Ducasse Stéphane Ducasse 1 Decorator.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Decorator Design Pattern Phillip Shin. Overview Problem Solution Example Key points.
Week 7, Class 1: The Command Pattern (cont.) Get Ready for Poll Everywhere Labs 2 & 3 returned Lab 7 due this evening at 11pm Quiz tomorrow at start of.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
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.
Introduction to Exceptions in Java CS201, SW Development Methods.
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
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
Slide design: Dr. Mark L. Hornick
Week 2, Day 1: The Factory Method Pattern
Factory Patterns 1.
Inheritance and Polymorphism
Object Oriented Programming
7. Decorator, Façade Patterns
Object Oriented Design Patterns - Structural Patterns
OO Design Patterns - Decorator
Decorator Pattern Richard Gesick.
Abstract Classes Page
SE-2811 Software Component Design
7. Decorator SE2811 Software Component Design
7. Decorator, Façade Patterns
Slide design: Dr. Mark L. Hornick
Week 8, Class 3: Model-View-Controller
Decorator Pattern.
Presentation transcript:

SE-2811 Dr. Mark L. Hornick 1

The Decorator Pattern SE-2811 Dr. Mark L. Hornick 2

Example: The “Ice Cream Store” 3 Ice Cream #description +getDescription() +cost() Cone cost() Dish cost() What kind of a class is Ice Cream ? What kind of a method is cost ? Why? Subclasses define their own implementation and return the cost of the IceCream. WaffleCone cost()

Decorator Pattern Intent You want to attach additional functionality to an (existing) class dynamically… …without having to resort to sub-classing the existing class  It’s also possible that the existing class is declared final, in which case you can’t subclass it …or making any modifications to the existing class  Also, you may not have access to the existing class’s source

The “Ice Cream Store” (contd.) 5 Ice Cream description getDescription() cost() Cone cost() Dish cost() public abstract class IceCream { String description; public String getDescription() { return description; } public abstract double cost(); } public class Cone extends IceCream { public Cone() { description = “Cone”; } public double cost() { return 1.24; }

Extending functionality Now in addition to your ice cream, you can also ask for additional toppings (Fudge, M&M’s, peanuts, …) Charges are extra for each additional topping. How should we design the system?

Alternative 1: Create a new class for each combination. Ice CreamToppings ConeM&M DishFudge WaffleConePeanuts Caramel Any problems with this approach?

Alternative 1: Create a new class for each combination. Ice CreamToppings ConeM&M DishFudge WaffleConePeanuts Caramel Results in class explosion! Maintenance nightmare! What happens when a new topping is added? What happens when the cost of a topping (e.g. Fudge) changes?

Alternative 2: Using instance variables to keep track of the toppings? 9 IceCream -description: String -hasFudge: boolean -hasMnM: boolean getDescription() +cost() +hasFudge() +hasCaramel()

Alternative 2 (contd.) 10 public class Icecream { ……………… public double cost() { double toppingCost = 0; if (hasFudge()) { toppingCost += oreoCost; } if (hasCaramel()) { toppingCost += caramelCost; } …. return toppingCost; }

Alternative 2 (contd.) 11 public class Cone{ ……………… public double cost() { return super.cost(); } What is this method calculating and returning?

So what’s the problem? We want to allow existing classes to be easily adapted to incorporate new behavior without modifying existing code. We want a design that is flexible enough to take on new functionality to meet changing requirements. The Decorator Pattern comes to the rescue!!

SE-2811 Dr. Mark L. Hornick 13

SE-2811 Dr. Mark L. Hornick 14

Using the Decorator Pattern Take the IceCream object Let’s create the Cone object. IceCream cone= new Cone(); Decorate it with the Fudge topping. Create a Fudge object and wrap it around Cone. cone = new Fudge(); Decorate it with the Nuts topping. Create a Nuts object and wrap it around Cone. cone = new Nuts(cone); Call the cost method and rely on delegation to add the cost of all toppings. Call cost on the outmost decorator. System.out.println(cone.cost());

Summary Decorators have the same super-type as the objects they decorate. One or more decorators can be used to wrap an object. Given that the decorator has the same super-type as the object it decorates, we can pass around a decorated object in place of the original (wrapped) object. The decorator adds its own behavior either before and/or after delegating to the object it decorates to do the rest of the job. Objects can be decorated at any time, so we can decorate objects at runtime with as many decorators as we like.