The TechGuy

April 28, 2011

Design Patterns by Examples – Decorator Pattern

Filed under: Uncategorized — Zeeshan Bilal @ 8:32 PM

This site has moved!

 

If your browser doesn’t take you there automatically in 5 seconds, please click on:

http://www.zishanbilal.com

 
Sorry for inconvenience…

Note: Please remember to update any links or bookmarks you may have about this site.

 

Thanks

19 Comments »

  1. Excellent material

    Comment by imranlatif — August 11, 2011 @ 11:28 AM

  2. Thanks ,Excellent material, Quite a helpful help, I must say. Beginners like us could learn a lot from you.

    Comment by Abdul Rauf — August 11, 2011 @ 5:40 PM

  3. brilliant material, definetly learnt a lot

    Comment by benn — August 12, 2011 @ 7:37 PM

  4. nice!

    Comment by Cyrille Chris — August 14, 2011 @ 10:19 PM

  5. Excuse me but let me state my objection/confusion regarding this example.
    The concrete decorators adds more functionality by adding more methods. That make them incompatible with each other. That doesn’t sound clear enough so lets take an example, for brevity consider classic “decorator pattern” example of windowing system. If you wish to add a horizontal scroll bar functionality you decorate it with HorizontalDecorator, and if you want to add Vertical scrollbar you decorate it with VerticalDecorator. Consider a case when you want to add the vertical scroll bar to a window already decorated with horizontal scrollbar. Now if both decorators do not follow same interface, its not possible. I hope I have made my point.

    Comment by Copper — August 15, 2011 @ 9:18 AM

  6. […] One of the best articles I had seen so far on decorator pattern Introduction This series of articles will help you to build a good understanding of design patterns using different examples from real life and some from well-known frameworks or APIs. There are many articles around the web that discuss design patterns but they sometimes lack appropriate examples to quote. So, either their purpose stays unclear or we cannot memorize the patterns longer and sooner they slip out of mind. More importantly, we unders … Read More […]

    Pingback by Design Patterns by Examples – Decorator Pattern (via Zeeshan Bilal’s Blog) « Deviltechie's Blog — August 23, 2011 @ 4:54 AM

  7. I liked your article as it is written in such a way that i easily memorize and request you to write more on other patterns.

    But one thing that i feel, which will improve it further, is implementation i.e. how to use this design. For example, how an employee will behave after becoming team lead and similarly to manager or vice versa.

    Comment by Fahad Khan — January 7, 2012 @ 11:05 AM

  8. excellent article

    Comment by shabir — March 15, 2012 @ 11:55 AM

  9. Let me differ from others but having entirely new methods in decorators (e.g. team lead, team member, team manager having additional resposibilities of assigning or monitoring tasks) should not be part of decorator. Moreover we cannot use these functionalities when we try to convert decorator into the component as these are not part of component. In this case decorated object would be incompatible with the component base object and we cannot use the additional functionality as correctly pointed out by Copper.
    Ideally decorator is used for mixing and matching between some functionalities and additional/ dynamic behaviour is exposed through some core property of method of base component class only.
    Had there been a code where we actually use decorated object, this thing would have become clearer (There was code only upto the declaration of decorated objects but no code where we create concrete component and then decorate them with decorator).

    Comment by Rajeev — April 11, 2012 @ 4:31 AM

  10. Nice post. Recently I posted another implementation of Decorator design pattern using C# and Order Manager as an example.
    Here is link to that post.

    http://www.nileshgule.com/2012/07/decorator-design-pattern.html

    Comment by Nilesh Gule — July 4, 2012 @ 4:02 PM

  11. Thanks for this great post. Its one of the very popular design pattern question in Java. By the here is my example of decorator pattern in Java.

    Comment by java67 — August 30, 2012 @ 7:37 PM

  12. excellent post! You may also include the nested instantiation of the objects in your team roles example.

    Comment by vassilis — August 31, 2012 @ 7:56 PM

  13. I just don’t get it, maybe someone could enlight me here. Why the abstract decorator must wrap AND extend the component interface? In your example i see no reason to extend the Employee interface, as the concrete decorators, ie Team Leader, Manager, deal with the roles of the Employees and not with the Employees themselfs… Furthermore, if you what to fire a Manager out of your company you could: Manager.getEmployee().terminate()… Why whould you need Manager.terminate(); when Manager is a role and terminate() refers to an Employee…

    Comment by aviaxisBogdan — October 3, 2012 @ 4:49 PM

  14. The reason is for been able to write the following:

    new ConcreateDecoratorA(new ConcreateDecoratorB(new ConcreteComponent()));

    This is the actually the purpose of this pattern: to alter in runtime the behavior of a ConcreteComponent via its wrapping by Decorators.

    Hope this helps..

    Cheers

    Comment by vassilis — November 12, 2012 @ 5:05 PM

  15. I am genuinely glad to glance at this weblog posts which carries
    tons of useful facts, thanks for providing such statistics.

    Comment by fusevision seo — April 28, 2013 @ 5:25 AM

  16. I think this is a great post. I agree with others that some implementation examples would have been good. When I think of the decorator pattern, I imagine a pattern in which you can add new methods and properties to a class that inherits from the same base class. Like merging the functionality of two classes. That isnt actually what it is, though. If I understand correctly, the decorator allows a class to add functionality to common methods.

    If, for example, TeamMember.terminate() looks like:
    public void terminate()
    {
    this.flipTable();
    }

    and TeamLead.terminate() looks like:
    public void terminate()
    {
    this.fireRestOfTeamOutOfSpite();
    }

    Then if you decorate the TeamMember with TeamLead, calling terminate will first flip a table, then fire the rest of his team out of spite.

    Does that sound right?

    Comment by Brian — May 14, 2013 @ 5:49 PM

  17. Excellent article. Keep posting such kind of info on your blog.
    Im really impressed by your site.
    Hello there, You have done a great job. I’ll certainly digg it and in my view suggest to my friends. I’m sure they’ll be benefited from this website.

    Comment by Melina — June 3, 2013 @ 10:29 AM

  18. Please add main () to add clarity, else it feels very incomplete.

    Comment by BJ — July 13, 2013 @ 11:16 AM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme. Create a free website or blog at WordPress.com.