Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Decorator Design Pattern

, 4 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Decorator Design Pattern Clearly Explained with Example

You can see this and other great articles on design patterns here.

The decorator design pattern allows you to add features to an object dynamically. An example would be the search functionality in an application. You may need to search for employees such as salary, zip code, skills, and so on. The user may choose to enter any combination of search criteria, and it would be a daunting task trying to figure out all the possible combinations as the number of fields grow. The decorator pattern allows you to dynamically add only the fields that the user is searching for.

In the decorator pattern, each feature is represented by a class. Therefore, if you have 10 features, then you will have 10 decorator classes.

Let’s look at the UML of the decorator pattern first, then we will look at an example to see how it works. Below is the UML of the decorator design pattern:

  • The IComponent interface defines the operation, or the features that the decorators can perform.
  • The StartComponent class is the starting object that you can dynamically add features to. You will create this object first and add features to it.
  • The Decorator class is an abstract class and is the parent class of all the decorators. While it implements the IComponent interface to define the operations, it also contains a private variable input that points to the object to be decorated. The input variable is simply assigned in the constructor.

The constructor for the Decorator class is simply:

public Decorator(IComponent i)
{ 
    input = i;
}
  • The ConcreteDecorator classes are the actual decorator classes that can add features. You can have as many ConcreteDecorator classes as you like, and each will represent a feature that can be added.

Let's do an example and see how it works. In our example, you have a plain ice cream where you can add different combinations of toppings to it. The UML will be:

  • The IComponent interface has the AddTopping method that all the classes implement.
  • The PlainIceCream class is the starting point object.
  • The Topping abstract class is the parent class of all the decorators. In its constructor, it assigns the parameter of type IComponent to the input variable, which points to the object to be decorated.
  • The PeanutTopping and the CandyTopping are the decorators, where each decorator has its own implementation on how to add the toppings.

The client code to use the decorators will be:

IComponent a = new PlainIceCream();    //your starting point

IComponent b = new PeanutTopping(a);  //notice you pass in the IComponent 
				   //that you want to decorate

IComponent c = new CandyTopping(b);    //add another decorator since the 
				    //user also likes candy

c.AddTopping(); 	//do all the operations of AddTopping() of 
		//PlainIceCream, PeanutTopping, and CandyTopping in one call

The code above allows you to add features dynamically using any combinations the user may choose.

The AddTopping method of the decorators will call previous decorators first, followed by the decoration that you define:

public void AddTopping()
{
    input.AddTopping();   //make the previous object do the decorations first
    //then add the implementation here to add the new feature
}

Below are the implementation code and the output of the decorator design pattern from our example. Notice that you can add any combinations of features dynamically in the client code:

class Program
{
    static void Main(string[] args)
    {
        IComponent a = new PlainIceCream();
        IComponent b = new CandyTopping(a);
        IComponent c = new PeanutTopping(b);
        IComponent d = new NutsTopping(c);
        d.AddTopping();
    }
}

public interface IComponent
{
    void AddTopping();
}

public class PlainIceCream : IComponent
{
    void IComponent.AddTopping()
    {
        Console.WriteLine("Plain Ice Cream ready for some toppings");
    }
}

public abstract class Topping : IComponent
{
    protected IComponent input;

    public Topping(IComponent i)
    {
        input = i;  //store the item to be decorated
    }

    void IComponent.AddTopping()
    {
    }
}

public class CandyTopping : Topping, IComponent
{
    public CandyTopping(IComponent i)
        : base(i)
    {
    }

    void IComponent.AddTopping()
    {
        input.AddTopping();  //decorate others first
        Console.WriteLine("Candy Topping added");
    }
}

public class PeanutTopping : Topping, IComponent
{
    public PeanutTopping(IComponent i)
        : base(i)
    {
    }

    void IComponent.AddTopping()
    {
        input.AddTopping();  //decorate others first
        Console.WriteLine("Peanut Topping added");
    }
}

public class NutsTopping : Topping, IComponent
{
    public NutsTopping(IComponent i)
        : base(i)
    {
    }

    void IComponent.AddTopping()
    {
        input.AddTopping();  //decorate others first
        Console.WriteLine("Nuts Topping added");
    }
}

Liked this article? You can see this and other great articles on design patterns here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DevLake

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy apologies PinmvpPete O'Hanlon5-May-11 4:38 
GeneralMy vote of 5 PinmentorKeith Barrow5-May-11 3:26 
GeneralMy vote of 1 Pinmemberblakecool5-May-11 2:35 
GeneralRe: My vote of 1 PinmemberCDP18025-May-11 3:07 
GeneralMy vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 23:21 
GeneralMy vote of 5 PinmemberCS20114-May-11 23:14 
GeneralPeople who have NOT published an article should NOT be allowed to vote [modified] PinmemberBill SerGio, Infomercial King4-May-11 21:06 
GeneralRe: People who have NOT published an article should NOT be allowed to vote [modified] PinmemberAddy Tas2-Apr-12 3:02 
GeneralMy vote of 5 PinmemberEdMan1964-May-11 10:50 
GeneralRe: My vote of 5 [modified] PinmemberEdMan1964-May-11 11:45 
GeneralRe: My vote of 5 PinmemberEdMan1964-May-11 12:26 
GeneralRe: My vote of 5 PinmvpAspDotNetDev4-May-11 12:31 
GeneralMy vote of 5 PinmvpPete O'Hanlon4-May-11 10:47 
GeneralRe: My vote of 5 PinmemberBill SerGio, Infomercial King4-May-11 11:16 
GeneralRe: My vote of 5 PinmemberCDP18024-May-11 21:13 
GeneralYou have never posted an article PinmemberBill SerGio, Infomercial King4-May-11 21:23 
GeneralRe: You have never posted an article PinmemberCDP18024-May-11 21:28 
GeneralRe: You have never posted an article PinmemberBill SerGio, Infomercial King4-May-11 22:44 
GeneralRe: You have never posted an article PinmemberCDP18024-May-11 22:52 
GeneralRe: You have never posted an article PinmemberBill SerGio, Infomercial King4-May-11 22:58 
GeneralRe: You have never posted an article PinmemberCDP18024-May-11 23:06 
GeneralRe: You have never posted an article PinmemberBill SerGio, Infomercial King4-May-11 23:13 
GeneralHope you are finished all this useless material PinmemberBill SerGio, Infomercial King4-May-11 10:18 
GeneralRe: Hope you are finished all this useless material [modified] PinmvpPete O'Hanlon4-May-11 10:41 
GeneralRe: Hope you are finished all this useless material PinmemberDaveAuld4-May-11 10:48 
GeneralRe: Hope you are finished all this useless material PinmvpPete O'Hanlon4-May-11 11:03 
GeneralYou wrote this terrible article- Going Solo - First Steps in Building a Successful Company, didn't you Pete? PinmemberBill SerGio, Infomercial King4-May-11 11:12 
GeneralRe: You wrote this terrible article- Going Solo - First Steps in Building a Successful Company, didn't you Pete? PinmvpPete O'Hanlon4-May-11 11:39 
GeneralRe: You wrote this terrible article- Going Solo - First Steps in Building a Successful Company, didn't you Pete? PinmvpAspDotNetDev4-May-11 12:11 
GeneralRe: You wrote this terrible article- Going Solo - First Steps in Building a Successful Company, didn't you Pete? PinmvpDave Kreskowiak4-May-11 11:46 
GeneralRe: Hope you are finished all this useless material PinmemberBill SerGio, Infomercial King4-May-11 11:05 
GeneralRe: Hope you are finished all this useless material PinmemberWes Aday4-May-11 13:43 
GeneralRe: Hope you are finished all this useless material PinmvpPete O'Hanlon4-May-11 13:53 
GeneralRe: Hope you are finished all this useless material PinmemberBill SerGio, Infomercial King4-May-11 23:00 
GeneralMy vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 9:12 
GeneralRe: My vote of 1 PinmemberMarc A. Brown4-May-11 9:23 
GeneralRe: My vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 9:46 
GeneralRe: My vote of 1 PinmemberMarc A. Brown4-May-11 10:08 
I am not posting anything other than the comment you replied to. It's not my blog post, so they're not my examples, good or not. That said, a blog post doesn't need to be as long or detailed as an article. I've only read this post from the author, though I see he's done quite a few of them, but I thought it was ok as a small blog post. Not great, but not terrible either. I also fail to see how it's spam. Did I miss a commercial pitch? I'd think with the title of "Infomercial King," you'd be better at identifying spam. Wink | ;)
GeneralRe: My vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 10:15 
GeneralRe: My vote of 1 PinmemberMarc A. Brown4-May-11 10:27 
GeneralI am impressed by your intelligent remarks PinmemberBill SerGio, Infomercial King4-May-11 12:50 
GeneralRe: I am impressed by your intelligent remarks PinmvpPete O'Hanlon4-May-11 13:28 
GeneralRe: My vote of 1 [modified] PinmemberEdMan1964-May-11 10:50 
GeneralRe: My vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 11:36 
GeneralRe: My vote of 1 PinmemberEdMan1964-May-11 11:55 
GeneralRe: My vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 12:21 
GeneralRe: My vote of 1 PinmemberEdMan1964-May-11 12:24 
GeneralRe: My vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 12:51 
GeneralRe: My vote of 1 PinmemberOakman4-May-11 18:16 
GeneralRe: My vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 21:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 4 May 2011
Article Copyright 2011 by DevLake
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid