Decorator Pattern.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
Chapter 3: The Decorator Pattern
CS 210 Introduction to Design Patterns September 12 th, 2006.
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.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Building Java Programs
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Composite Design Pattern. Motivation – Dynamic Structure.
Multiple Choice Solutions True/False a c b e d   T F.
Design Patterns.
Unit 13 Decorator Summary prepared by Kirk Scott 1.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns.
Prof. Hertz (as told by xkcd.com)‏. Computer Science 313 – Advanced Programming Topics.
Factory Method A Creational Design Pattern. Factory Method Key Features  Defines an interface for creating objects without needing to know each object’s.
Nov 2005 MSc Slide 1 First we look at a standard Dos based application Combining Decorator & Factory Patterns.
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.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.
Creational Patterns
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Nested for loops.
Lecture 14 Inheritance vs Composition. Inheritance vs Interface Use inheritance when two objects share a structure or code relation Use inheritance when.
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.
Dynamic Programming & Memoization. When to use? Problem has a recursive formulation Solutions are “ordered” –Earlier vs. later recursions.
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.
Dayu Zhang 9/10/2014 Lab03. Outline Brief Review of the 4 Steps in Hello.cpp Example Understand endl and \n Understand Comment Programming Exercise -
Deriving Abstract Factory Loosening the coupling when creating objects...
Decorator Pattern ¢ Design: Which classes must be included?  Story As a user I want a weekend planner so that I can have more fun  Scenario Given.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
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.
Façade and the DP Wrap-Up 1. Which pattern does this class diagram from the Factory chapter call out for? A.Strategy B.Decorator C.Adapter D.Factory 2.
Summary prepared by Kirk Scott
9.1 Class (static) Variables and Methods
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Summary prepared by Kirk Scott
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Factory Method, Abstract Factory, and More
Part I General Principles
Building Java Programs
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
OO Design Patterns - Decorator
Decorator Pattern Richard Gesick.
Review: Design Pattern Structure
Functional interface.
Arithmetic Sequence Objective:
The System.exit() Method
Structural Patterns: Adapter and Bridge
7. Decorator SE2811 Software Component Design
2009 Test Key.
Presentation transcript:

Decorator Pattern

Decorator Pattern Can you say “composition” and “delegation”? Again???

Decorator Pattern - Problem Want to allow adding new behaviors to an object without modifying class Coffee condiments example is great Soy, Mocha, Whip, … Class for each combination? 2n is too many! And would be static – cannot change on the fly Flags in Beverage? Many combos are nonsense Have to modify Beverage to add new options Clothing another example Take off jacket? Put on hat? 2 flags or 4 classes? No! How do w/o mod’ing existing classes? Tea: Options make sense?

Decorator - Solution By composing and delegating, Condiments are “decorating” the cost and description

UML for Coffee Decorator Composes (HAS-A) Extends

In family of patterns called “Wrapper” UML for Decorator In family of patterns called “Wrapper”

Code a Capitalization Decorator Example: hello earthlings  Hello Earthlings class WordReader extends Reader { public WordReader (File file); public int read (char[] cbuf); // cbuf holds word } You are decorating the WordReader class (or rather, it’s objects) with the capitalization capability. Nesting (composing) a WordReader inside CapitalizationDecorator Useful resource: static char Character.toUpperCase(char)

Capitalization Decorator class WordReader extends Reader { public WordReader (File file); public int read (char[] cbuf); // cbuf holds word } class UpperReaderDecorator extends ReaderDecorator { Reader innerReader; public UpperReaderDecorator(Reader iR) { innerReader = iR; } public int read (char[] cbuf) { int length = innerReader.read(cbuf); cbuf[0] = Character.toUpperCase(cbuf[0]); return length; } } char[100] mybuf; Reader myreader = new WordReader(System.in); myreader = new UpperReaderDecorator(myreader); int length = myreader.read(mybuf); abstract class ReaderDecorator extends Reader { abstract public int read (char[] cbuf); }