Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University

Slides:



Advertisements
Similar presentations
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
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.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
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.
The Decorator Design Pattern (also known as the Wrapper) By Gordon Friedman Software Design and Documentation September 22, 2003.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Composite Design Pattern. Motivation – Dynamic Structure.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Design Patterns.
Department of Computer Science, York University Object Oriented Software Construction 16/09/ :52 PM 0 COSC3311 – Software Design Decorator Pattern.
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.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
GoF: Document Editor Example Rebecca Miller-Webster.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
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.
Design Patterns Structural Patterns. Adapter Convert the interface of a class into another interface clients expect Adapter lets classes work together.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
1 Advanced Object-oriented Design – Principles and Patterns Structural Design Patterns.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
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.
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.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
S.Ducasse Stéphane Ducasse 1 Adapter.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns: MORE Examples
Abstract Factory Pattern
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
More Design Patterns 1.
More Design Patterns 1.
Decorator Intent Also known as Wrapper Example: a Text Window
Design Patterns A Case Study: Designing a Document Editor
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.
Behavioral Design Pattern
BRIDGE PATTERN.
Structural Patterns: Adapter and Bridge
Strategy Design Pattern
Decorator.
Informatics 122 Software Design II
Decorator Pattern.
Adapter Pattern Jim Fawcett
Software Design Lecture 10.
Adapter Pattern Jim Fawcett
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Decorator Pattern Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University

Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Also Known As: Wrapper

Motivation(1) Sometimes we want to add responsibilities to individual objects, not to an entire class. Inheritance is inflexible because the responsibilites statically. A more flexible approach is to enclose the component in another object that adds the responsibilites. The enclosing object is called Decorator.

Motivation(2) The decorator conforms to the interface of the component it decorates so that its presence is transparent to the clients. The decorator forwards requests to the component and may perform additional actions before and/or after forwarding. Transparency lets you nest decorators recursively.

Motivation(3) Decorator subclasses are free to add operations for specific functionality. This pattern lets decorators appear anywhere a VisualComponent can.

Applicability Use Decorator to add responsibilities to individual objects dynamically and transparently. for responsibilities that can be withdrawn. When extension by subclassing is impractical.

Structure Component Operation() Decorator Operation() Operation() ConcreteComponent Decorator Operation() Operation() ConcreteDecoratorA ConcreteDecoratorB Operation() AddedState Operation()

Participants(1) Component Concrete Component defines the interface for objects that can have responsibilities added to them dynamically. Concrete Component defines an object to which additional responsibilities can be attached.

Participants(2) Decorator Concrete Decorator maintains a reference to a Component object and defines an interface that conforms to Component’s interface. Concrete Decorator adds responsibilities to the component.

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

Consequences(1) More flexibility than static inheritance. responsibilities can be added and removed at run-time simply by attaching and detaching them. Furthermore, providing different Decorator classes for a specific Component class lets you mix and match responsibilities.

Consequences(2) Avoids feature-laden classes high up in the hierarchy. Offers a pay-as-you-go approach to adding responsibilities. You can define a simple class and add functionality incrementally with Decorator objects. Also easy to define new kinds of Decorators independently.

Consequences(3) A decorator and its component aren’t identical. From an object identity point of view, a decorated component is not identical to the component itself. Hence you shouldn’t rely on object identity when you use decorators.

Consequences(4) Lots of little objects Often results in systems composed of lots of little objects that all look alike. The objects differ only in the way they are interconnected, not in their class or in the value of their variables. Although these systems are easy to customize by those who understand them, they can be hard to learn and debug.

Implementation(1) Interface conformance A decorator object’s interface must conform to the interface of the component it decorates. ConcreteDecorator classes must inherit from a common class. Omitting the abstract Decorator class There’s no need to define an abstract Decorator class when you only need to add one responsibility. That’s often the case when you’re dealing with an existing class hierarchy rather than designing a new one.

Implementation(2) Keeping Component classes lightweight. Components and decorators must descend from a common Component class. It’s important to keep this common class lightweight: focus on defining an interface, not on storing data. Putting a lot of functionality into component also increases the probability that concrete subclasses will pay for features they don’t need.

Implementation(3) Changing the skin of an object versus changing its guts think of a decorator as a skin over an object that thanges its behavior. An alternative is to change the object’s guts like Strategy pattern does. Strategies are a better choice in situations where the Component class is intrinsically heavyweight, thereby making the Decorator pattern too costly.

Implemention(4) Changing the skin of an object versus changing its guts Decorator pattern only changes a component from the outside, the component doesn’t have to know anything about its decorator. With strategies, the component itself knows about possible extensions. A strategy can have its own specialized interface, whereas a decorator’s interface must conform to the component’s.

Sample Code(1) Sample code for the base Component class class VisualComponent { public: VisualComponent(); virtual void Draw(); virtual void Resize(); }

Sample Code(2) Sample code for base Decorator class Decorator : public VisualComponent {public: Decorator(VisualComponent *); virtual void Draw(); virtual void Resize(); private: VisualComponent * _component; }

Sample Code(3) The default implementation that passes the request on to _component: void Decorator::Draw(){ _component->Draw(); } void Decorator::Resize(){ _component->Resize();

Sample Code(4) Sample code for concrete decorator class BorderDecorator: public Decorator{ public: BorderDecorator(VisualComponent*, int borderWidth); virtual void Draw(); private: void DrawBorder(int); int _width; } void BorderDecorator::Draw(){ Decorator::Draw(); DrawBorder(_width);

Sample Code(5) Use decorators void Window::SetContents(VisualComponent * contents){ … } Window *window = new Window; window->SetContents( new BorderDcorator( new ScrollDecortor(textView), 1 ) );

Know Uses Many object-oriented user interface toolkits use decorators to add graphical embellishment to widgets. Streaming classes.