.NET Design Patterns The decorator pattern. Adding behavior at runtime

added by bjames
8/27/2012 8:41:14 PM

5 Kicks, 507 Views

Welcome to the first of many articles on design patterns on refactorthis.net. This article explains The decorator pattern. Adding new behavior to your existing objects at run time.


8/29/2012 7:42:27 AM
Why have you closed the comment feature in your blog?

Decorators (well, classes in general, but especially decorators) should not have init methods (SetArticle) but take the decorated instance in the constructor.

8/29/2012 8:32:42 PM
Thanks for reading. If you look at my blog you will notice that I have both a setter method as well as an overloaded constructor.

As a matter of fact, the example code uses the overloaded constructor for the Tweetable class.

From my blog..
public Tweetable(){ }

/// <summary>
/// A emthod to provide the article to be extended.
/// </summary>
/// <param name="article">The article in which to add new behavior</param>
public Tweetable(Article article)
_article = article;

I included both just to show that you have options.

As far as the comments are concerned, I found that the spam is too much trouble.

Thanks again for reading. I'll remove the setter method from the example since it's not being used. This will avoid confusion.

Thanks again.


8/30/2012 12:06:40 AM
great. you just got kicked. You can read more about init methods here: http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling.aspx