S.Ducasse Stéphane Ducasse 1 Decorator.

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.
02 - Structural Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
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.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Fall 2009ACS-3913 Ron McFadyen1 Decorator Pattern The Decorator pattern allows us to enclose an object inside another object. The enclosing object is called.
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
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 Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
The Decorator Design Pattern (also known as the Wrapper) By Gordon Friedman Software Design and Documentation September 22, 2003.
Composite Design Pattern. Motivation – Dynamic Structure.
Design Patterns.
1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 Software Design Adapter Pattern Façade pattern.
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.
Prof. Hertz (as told by xkcd.com)‏. Computer Science 313 – Advanced Programming Topics.
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.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Stéphane Ducasse 1 Visitor.
Architectural pattern: Interceptor Source: POSA II pp 109 – 140POSA II Environment: developing frameworks that can be extended transparently Recurring.
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.
Structural Design Patterns
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.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
1 Advanced Object-oriented Design – Principles and Patterns Structural Design Patterns.
Stéphane Ducasse 1 Strategy.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Decorator Design Pattern Phillip Shin. Overview Problem Solution Example Key points.
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
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.
S.Ducasse Stéphane Ducasse 1 Abstract Factory.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns: MORE Examples
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
CSE 331 Software Design & Implementation
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Decorator Design Pattern
More Design Patterns 1.
More Design Patterns 1.
Decorator Intent Also known as Wrapper Example: a Text Window
Design Pattern Detection
Decorator Pattern Intent
Jim Fawcett CSE776 – Design Patterns Summer 2003
Informatics 122 Software Design II
Object Oriented Design Patterns - Structural Patterns
Decorator Pattern Richard Gesick.
UNIT-III Structural Design Patterns
BRIDGE PATTERN.
Structural Patterns: Adapter and Bridge
Decorator.
Informatics 122 Software Design II
Decorator Pattern.
Adapter Pattern Jim Fawcett
Decorator Pattern The decorator pattern allows us to enclose an object inside another object. The enclosing object is called a decorator. The other object.
Software Design Lecture 10.
Adapter Pattern Jim Fawcett
Presentation transcript:

S.Ducasse Stéphane Ducasse 1 Decorator

S.Ducasse License: CC-Attribution-ShareAlike

S.Ducasse 3 Decorator Attach additional responsib ilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Aka: Wrapper

S.Ducasse 4 Decorator Intent Attach additional responsib ilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Aka: Wrapper

S.Ducasse 5 Motivation Adding behavior to individual object not entire class scrollbar border

S.Ducasse 6 Inheritance? Does not work well Too much combination border, color, scrollbars, bounds, translation... To static: Clients cannot control when to put a border or not...

S.Ducasse 7 Decorator Enclose the component into another one that adds border...in another one that adds scrollbar... The decorator conforms to the interface of the component it decorates so that its presence is transparent to the component's clients. The decorator forwards requests to the component and may perform additional actions (such as drawing a border) before or after forwarding

S.Ducasse 8 Decorator Solution

S.Ducasse 9 Applicability When responsibilities can be withdrawn When responsibilities can be added transparently When subclassing is not possible (combination explosion)

S.Ducasse 10 Possible Decorator Structure

S.Ducasse 11 Participants Component (VisualComponent) defines the interface for objects that can have responsibilities added to them dynamically. ConcreteComponent (TextView) defines an object to which additional responsibilities can be attached. Decorator maintains a reference to a Component object and defines an interface that conforms to Component's interface. ConcreteDecorator (BorderDecorator, ScrollDecorator) adds responsibilities to the component.

S.Ducasse 12 Collaborations Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request.

S.Ducasse 13 About Identity A decorator and its component aren't identical. A decorator acts as a transparent enclosure. But from an object identity point of view, a decorated component is not identical to the component itself. If the decorator is wrapping, then identity of the object may change. Good at construction time, but else should “adapt” the references from the decorated to the decorator.

S.Ducasse 14 Consequences More flexibility than static inheritance. Dynamic addition of properties Avoids feature-laden classes high up in the hierarchy. Lots of little objects.

S.Ducasse 15 Implementation Interface conformance. A decorator object's interface must conform to the interface of the component it decorates. ConcreteDecorator classes must therefore inherit from a common class (at least in C++). Omitting the abstract Decorator class. There's no need to define an abstract Decorator class when you only need to add one responsibility. Keeping Component classes lightweight. Component should specify an interface, decorators are then easier to define

S.Ducasse 16 Wrapping or not? Conforming or not With decorator With strategies

S.Ducasse 17 Strategies? Strategies are a better choice when the Component class is heavyweight, thereby making the Decorator pattern too costly to apply. The Strategy-based approach might require modifying the component to accommodate new extensions. a strategy can have its own specialized interface, a decorator's interface must conform to the component's. A strategy needs only define the interface for rendering a border, which means that the strategy can be lightweight even if the Component class is heavyweight.

S.Ducasse 18 Known Uses VisualWorks Wrapper hierarchy Stream Decorators in VisualWorks: BOSSTransporter is a stream decorator FormattedStream is a stream decorator