The Decorator Design Pattern (also known as the Wrapper) By Gordon Friedman Software Design and Documentation September 22, 2003.

Slides:



Advertisements
Similar presentations
Chapter 3: The Decorator Pattern
Advertisements

Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?
DECORATOR by Ramani Natarajan Also known as ‘Wrapper.’ Also known as ‘Wrapper.’ According to ‘gang of four’(sounds like an Akira Kurosawa movie): A Decorator.
Decorator & Chain of Responsibility Patterns Game Design Experience Professor Jim Whitehead February 2, 2009 Creative Commons Attribution 3.0 (Except for.
Fall 2009ACS-3913 Ron McFadyen1 Decorator Pattern The Decorator pattern allows us to enclose an object inside another object. The enclosing object is called.
Sept 2004Ron McFadyen Decorator Pattern The decorator pattern allows us to enclose an object inside another object. The enclosing object is called.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Exam Questions Chain of Responsibility & Singleton Patterns Game Design Experience Professor Jim Whitehead February 4, 2009 Creative Commons Attribution.
Design Pattern – Bridge (Structural) References Yih-shoung Chen, Department of Information Engineering, Feng Chia University,Taiwan, R.O.C. The Bridge.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
Winter 2011ACS-3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
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.
Objects and Components. The adaptive organization The competitive environment of businesses continuously changing, and the pace of that change is increasing.
Department of Computer Science, York University Object Oriented Software Construction 16/09/ :52 PM 0 COSC3311 – Software Design Decorator Pattern.
Structural Pattern: Decorator There are times when the use of subclasses to modify the behavior of individual objects is problematic. C h a p t e r 4.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
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).
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
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.
GoF: Document Editor Example Rebecca Miller-Webster.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
Chapter 12 Support for Object oriented Programming.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
Design Patterns: Elements of Reusable Object- Orientated Software Gamma, Helm, Johnson, Vlissides Presented By: David Williams.
An Introduction To Design Patterns Jean-Paul S. Boodhoo Independent Consultant
Chain of Responsibility Behavioral Pattern. Defination Avoid coupling between the sender and receiver by giving more than one object a chance to handle.
1 Advanced Object-oriented Design – Principles and Patterns Structural Design Patterns.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
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.
S.Ducasse Stéphane Ducasse 1 Adapter.
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.
ISBN Chapter 12 Support for Object-Oriented Programming.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns: MORE Examples
Strategy Design Pattern
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Factory Patterns 1.
More Design Patterns 1.
More Design Patterns 1.
Decorator Intent Also known as Wrapper Example: a Text Window
Design Pattern Detection
Decorator Pattern Intent
Informatics 122 Software Design II
Decorator Pattern Richard Gesick.
Software Design Lecture : 14.
Decorator.
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Informatics 122 Software Design II
Decorator Pattern.
Decorator Pattern The decorator pattern allows us to enclose an object inside another object. The enclosing object is called a decorator. The other object.
Presentation transcript:

The Decorator Design Pattern (also known as the Wrapper) By Gordon Friedman Software Design and Documentation September 22, 2003

Overview of Decorators The decorator pattern provides a viable alternative to subclassing Subclassing allows individual objects to take on responsibilities that would not normally be available to the entire class Subclassing is inflexible because the responsibilities are decided statically through inheritance Every instance of the subclass has identical responsibilities which cannot be changed at runtime The decorator allows additional responsibilities to be added to an object dynamically, circumventing the drawbacks of subclassing

Overview of Decorators The decorator object encloses a particular component and then adds responsibilities Conforms to the interface of the enclosed component creating transparency towards the clients Transparency allows many decorators to be nested recursively giving the potential of an indefinite amount of added responsibilities

Example: TextView Object The TextView Object in the diagram is the abstract class which is given responsibilities by two decorator objects aBorderDecorator draws a border around the TextView object aScrollDecorator gives the TextView object functional scroll bars

Example: TextView Object The class diagram shows that the ScrollDecorator and BorderDecorator classes are both subclasses of the abstract Decorator class

Applications of Decorators Dynamically and Transparently adds responsibilities to objects Use Decorators when you have responsibilities which can be removed Decorators can be used when subclassing becomes too complicated and involved

Decorator Class Diagram A general view of the decorator class diagram

Drawbacks of Decorators Although a decorator is transparent towards its component object, they are not identically the same Cannot rely on object identity Projects which contain Decorators often have many little objects which appear to all look alike. Programs which use Decorators are easily customized by the original programmer, but end up being extremely difficult to debug by anyone else.

Pros Of Decorators A decorator is more flexible than the inheritance alternative Responsibilities can be added and detached in run-time Retains the ability to add functionality incrementally from simple pieces Do not need to know all foreseeable features of the class before it is built

Things to Consider The decorator interface must conform to the interface of the decorated component When only one responsibility is needed, the abstract decorator class can be omitted. The decorator class would go in it’s place on the class diagram The component class should be kept low functional and focused on the interface. Data defining should be done in the subclasses. A complex component class will make decorators heavyweight and less versatile

Sample Code: Decorator Component Class called “VisualComponent”

Sample Code: Decorator Subclass of VisualComponent called “Decorator” Decorator subclass is able to reference VisualComponent through the _component variable

Sample Code: Decorator Passing functions Requests are passed to _component

Sample Code: Decorator Subclass of the Decorator class, “BorderDecorator” Specifies individual operations, draws the border on the VisualComponent Inherits all operation implementations from Decorator

References Gamma, Helm, Johnson, and Vlissides. Design Patterns. Addison-Wesley, Reading, MA, 1995 Steven Black, Design Pattern: Decorator. Antonio Garcia and Stephen Wong, The Decorator Design Pattern. ecoratorPattern.htm ecoratorPattern.htm

Any Questions?