67 %
33 %
Information about decorator

Published on November 22, 2007

Author: Naples


DECORATOR by Ramani Natarajan:  DECORATOR by Ramani Natarajan Also known as ‘Wrapper.’ According to ‘gang of four’(sounds like an Akira Kurosawa movie): A Decorator is used “to attach additional responsibilities to an object dynamically. A Decorator provides a flexible alternative to subclassing for extending functionality.” It is a ‘Structural Object Pattern.’ Structural Class patterns like ‘Adapter’ are concerned with how classes and objects are composed using inheritance. They are static Decorator (contd.):  Decorator (contd.) compared to structural object patterns like Decorator that describe ways to compose objects to realize new functionality. Such a structural object pattern gives the ability to change compositions at run-time. It allows open-ended number of additional responsibilities, like adding borders, shadow, instill scrolling, zooming…….. What am I talking about?:  What am I talking about? Consider the class interface for the universal set { x | x is a frog }: DEFINE CLASS Frog AS CUSTOM FUNCTION Jump(nValue) FUNCTION Eat( ) ENDDEFINE Suppose we want to make Kermit dance; we subclass as: DEFINE CLASS DancingFrog AS Frog FUNCTION Dance( ) ENDDEFINE What am I talking about?(contd):  What am I talking about?(contd) Now suppose Kermit wants to sing and dance, we have several options: We could augment DancingFrog with the Sing( ) method, perhaps renaming the class to EntertainingFrog, or We could subclass DancingFrog and add a Sing( ) method to the subclass DancingSingingFrog, or We could make all Frogs sing by augmenting the original Frog class, or (This is getting out of hand!!) we could simplify the exploding hierarchies by giving every Frog the Sing( ) and Dance( ) methods in a single, static, stiff, monolithic, gargantuan Frog class. What am I talking about?(contd):  What am I talking about?(contd) Now what if Kermit wants to teach “Object Oriented Architecture, Design and Methodology?” ENTER DECORATOR Consider the abstract Decorator class: DEFINE CLASS DecoFrog AS CUSTOM RealFrog = NULL FUNCTION INIT(Frog) THIS.RealFrog = Frog FUNCTION Jump(n) THIS.RealFrog.Jump(n) FUNCTION Eat( ) THIS.RealFrog.Eat( ) ENDDEFINE What am I talking about?(contd):  What am I talking about?(contd) If we need a specialized one-off Frog, we could DEFINE CLASS DecoSingingFrog AS DecoFrog FUNCTION Sing( ) ? WAIT WINDOW “Que Sera Sera Whatever will be will have been Future’s not mine to be seen It’s not easy being Green” What I am Saying:  What I am Saying Similarly we could substitute a singing and dancing Frog at run-time as: Kermit = CREATE(Frog) Kermit = CREATE(“DecoSingingFrog”, Kermit) Kermit = CREATE(“DecoDancingFrog”, Kermit) Note: Decorators can be chained with functionality added or modified as needed; as done above. Voila! We now have Frog Kermit that is instantiated to sing and dance and we didn’t need to pollute the Frog class; in fact, we didn’t need the source to class Frog. Advantages:  Advantages More Flexible than static class inheritance. Responsibilities can be added and subtracted at run-time simply by attaching and detaching them Easy to add functionality n number of times by simply attaching responsibilities n number of times. Avoids feature heavy classes high up in the hierarchy. We can define a simple class and add functionality as we go along. A microcosmic example of the philosophy of eXtreme Programming. A class can be extended by simply knowing its interface, source is usually not required. Depending upon the situation, we can use Decorators stoked on Wheaties, or Decorators munching on Rice Krispies, or any Decorator in-between to accomplish that particular task Disadvantages:  Disadvantages A Decorator and its component aren’t identical. Relying on object identity will make the programmer plead Mea Culpa. Lots of little objects like the flesh-eating scarabs in the movie Mummy. Too many Decorator objects will make the program confusing and hard to debug. It takes more time to instantiate an object if one also instantiates one or more Decorators, and once created, it takes slightly more time for messages to filter through layers of Decorators to reach the object being decorated. Conclusion:  Conclusion Decorators change the class appearance unlike subclassing that changes the class internals. Decorators have their interfaces identical to the object that it decorates. A Decorator adds functionality either before or after the call to the object that the Decorator contains. Decorators can be nested to allow for an infinite amount of customization. Decorators are not solutions for every situation, but occasionally, just occasionally, they are the ticket.

Add a comment

Related presentations

Related pages

Decorator – Wikipedia

Der Decorator (auch Dekorierer) ist ein Entwurfsmuster aus dem Bereich der Softwareentwicklung und gehört zur Kategorie der Strukturmuster (engl.
Read more

AKVIS Decorator füllt Flächen mit neuen Texturen und Farben.

AKVIS Decorator erlaubt es, die Oberfläche eines Objekts zu verändern. Wählen Sie einen Teil des Bildes aus und wenden neue Texturen darauf an.
Read more

Das Decorator Design Pattern (Dekorierer Entwurfsmuster))

Eine ausführliche Analyse des Decorator Design Pattern (Dekorierer Entwurfsmuster) mit Einführung und Diskussion.
Read more

Decorator .NET Design Pattern in C# and VB -

Decorator .NET Design Pattern C# and VB. Free source code and UML
Read more

Decorator-Klasse (Microsoft.VisualStudio.Modeling.Diagrams)

Decorator-Elemente werden um andere Elemente im Diagramm eingeschlossen, um diese zu formatieren. Um sicherzustellen dass innere Decorator-Elemente sich ...
Read more

Decorator pattern - Wikipedia, the free encyclopedia

The decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of ...
Read more

Decorator | Definition of Decorator by Merriam-Webster

Seen and Heard. What made you want to look up decorator? Please tell us where you read or heard it (including the quote, if possible).
Read more

Decorator Tutorial. Schneekönigin.

Mit AKVIS Decorator kann man Flächen eines Fotos mit neuen Farben bzw Texturen füllen. In diesem Tutorial wandeln wir ein Mädchen in eine Schneekönigin um.
Read more

DECORATOR | Official Decorator Website

BIO. The term DECORATOR has officially been redefined, though Merriam Webster may not accept the new entry. DECORATOR – an infectious alt-rock band that ...
Read more

Decorator-Konstruktor (System.Windows.Controls)

Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
Read more