Geeks With Blogs
A Curious Mind #tastic

Intent: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.*

This post is another love story about software patterns. I love programming to interfaces. Today I am going to talk about one of my favorite benefits of doing this, leveraging the Decorator pattern. What follows is a simple class getting refactored/developed to use the Decorator pattern.

Take for example this simple class:

public class Repository
{
    public void Save(Product product)
    {
        // do stuff
    }
}

Lets suppose you want to add logging to this class. What do you do? This maybe?

public class Repository
{
    ILog log = LogManager.GetLogger(typeof(Repository));
    public void Save(Product product)
    {
        if(log.IsDebugEnabled) log.Debug("Some nifty message here");
        // do stuff
    }
}

However, now we have violated the principal of Seperation of Concerns. If we refactor the original class to an interface we get:

public interface Repository
{
    void Save(Product product);
}

public class DefaultRepository : Repository
{
    public void Save(Product product)
    {
        // do stuff
    }
}

Yes, I did not put an "I" on the interface on purpose. Now that we have the interface we can add this class:

public class LoggingRepositoryDecorator
{
    ILog log = LogManager.GetLogger(typeof(Repository));
    Repository _inner;
    
    public LoggingRepositoryDecorator(Repository repository)
    {
        this._inner = repository;
    }
    public void Save(Product product)
    {
        if(log.IsDebugEnabled) log.Debug("Some nifty message here");
        this._inner.Save(product);
    }
}

Now we have seperated our concerns (Yea!) and demonstrated the Decorator pattern. You use it like this:

Repository repo = new LoggingRepositoryDecorator(new DefaultRepository());

See how it gets decorated or "wrapped?" The client code has no concept that there is logging involved. If you use some kind of factory to generate the Repositor instance you can turn the logging on and off quite easily by either wrapping or not wrapping the Repository instance. For some examples chech out Ayende's Rhino.Commons. You can do alot of infrastrutry things this way, security, logging, tracing, etc. This is also a kind of poorman's AOP.

Also Head First Design Patterns does a great job explaining this pattern.

Random paper about Programming to Interfaces in Java at KSU

Joel on Software forum posting "Why program to interfaces"

Posted on Wednesday, September 6, 2006 6:34 PM | Back to top


Comments on this post: The Decorator Pattern

# re: The Decorator Pattern
Requesting Gravatar...
I like AOP a lot better for seperation of concerns. Look at Sprin.net, those guys have a nice framework starting to shape up. You could also use an Attribute class & reflection to mark a class loggable, & mark methods to log, later reflecting to find them. Kinda hack-y, but the syntax is a lot nicer.
Left by sigvicious on Sep 06, 2006 10:28 PM

# re: The Decorator Pattern
Requesting Gravatar...
sigvicious,

I too prefer AOP for seperation of concerns and both spring.net and castle's microkernel have some very nice AOP intercepters. One thing that I don't like about Attributes is that you have to recompile the code to change the Attribute. I much prefer to use configuration files for stuff that i want to turn on and off. does that make sense?
Left by dru on Sep 07, 2006 1:14 AM

# re: The Decorator Pattern
Requesting Gravatar...
why didn't you Inherit the LoggingRepositoryDecorator
from Repository ???

please clarify this.. as the in the decorator pattern the Decorator aggregates the component and inherits the component aswell
Left by Yasser Shahzad on Jul 29, 2008 2:56 AM

# re: The Decorator Pattern
Requesting Gravatar...
I don't know why, you would want to inherit at all. Inheritance, would not allow me to add and remove the decorators at will while the program was running. Where do you see (book, article) that the decorator is a pattern of inheritance rather than composition?

-d
Left by Dru on Jul 29, 2008 8:58 AM

Your comment:
 (will show your gravatar)


Copyright © Dru Sellers | Powered by: GeeksWithBlogs.net | Join free