Click here to Skip to main content
Click here to Skip to main content

Decorator Design Pattern

By , 4 May 2011
 

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)

About the Author

DevLake
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy apologiesmvpPete O'Hanlon5 May '11 - 3:38 
GeneralMy vote of 5mentorKeith Barrow5 May '11 - 2:26 
GeneralMy vote of 1memberblakecool5 May '11 - 1:35 
GeneralRe: My vote of 1memberCDP18025 May '11 - 2:07 
GeneralMy vote of 1memberBill SerGio, Infomercial King4 May '11 - 22:21 
GeneralMy vote of 5memberCS20114 May '11 - 22:14 
GeneralPeople who have NOT published an article should NOT be allowed to vote [modified]memberBill SerGio, Infomercial King4 May '11 - 20:06 
GeneralRe: People who have NOT published an article should NOT be allowed to vote [modified]memberAddy Tas2 Apr '12 - 2:02 
GeneralMy vote of 5memberEdMan1964 May '11 - 9:50 
GeneralRe: My vote of 5 [modified]memberEdMan1964 May '11 - 10:45 
GeneralRe: My vote of 5memberEdMan1964 May '11 - 11:26 
GeneralRe: My vote of 5mvpAspDotNetDev4 May '11 - 11:31 
GeneralMy vote of 5mvpPete O'Hanlon4 May '11 - 9:47 
GeneralRe: My vote of 5memberBill SerGio, Infomercial King4 May '11 - 10:16 
GeneralRe: My vote of 5memberCDP18024 May '11 - 20:13 
GeneralYou have never posted an articlememberBill SerGio, Infomercial King4 May '11 - 20:23 
GeneralRe: You have never posted an articlememberCDP18024 May '11 - 20:28 
GeneralRe: You have never posted an articlememberBill SerGio, Infomercial King4 May '11 - 21:44 
GeneralRe: You have never posted an articlememberCDP18024 May '11 - 21:52 
GeneralRe: You have never posted an articlememberBill SerGio, Infomercial King4 May '11 - 21:58 
GeneralRe: You have never posted an articlememberCDP18024 May '11 - 22:06 
GeneralRe: You have never posted an articlememberBill SerGio, Infomercial King4 May '11 - 22:13 
GeneralHope you are finished all this useless materialmemberBill SerGio, Infomercial King4 May '11 - 9:18 
GeneralRe: Hope you are finished all this useless material [modified]mvpPete O'Hanlon4 May '11 - 9:41 
GeneralRe: Hope you are finished all this useless materialmemberDaveAuld4 May '11 - 9:48 
GeneralRe: Hope you are finished all this useless materialmvpPete O'Hanlon4 May '11 - 10:03 
GeneralYou wrote this terrible article- Going Solo - First Steps in Building a Successful Company, didn't you Pete?memberBill SerGio, Infomercial King4 May '11 - 10:12 
GeneralRe: You wrote this terrible article- Going Solo - First Steps in Building a Successful Company, didn't you Pete?mvpPete O'Hanlon4 May '11 - 10:39 
GeneralRe: You wrote this terrible article- Going Solo - First Steps in Building a Successful Company, didn't you Pete?mvpAspDotNetDev4 May '11 - 11:11 
GeneralRe: You wrote this terrible article- Going Solo - First Steps in Building a Successful Company, didn't you Pete?mvpDave Kreskowiak4 May '11 - 10:46 
GeneralRe: Hope you are finished all this useless materialmemberBill SerGio, Infomercial King4 May '11 - 10:05 
GeneralRe: Hope you are finished all this useless materialmemberWes Aday4 May '11 - 12:43 
GeneralRe: Hope you are finished all this useless materialmvpPete O'Hanlon4 May '11 - 12:53 
GeneralRe: Hope you are finished all this useless materialmemberBill SerGio, Infomercial King4 May '11 - 22:00 
GeneralMy vote of 1memberBill SerGio, Infomercial King4 May '11 - 8:12 
GeneralRe: My vote of 1memberMarc A. Brown4 May '11 - 8:23 
GeneralRe: My vote of 1memberBill SerGio, Infomercial King4 May '11 - 8:46 
GeneralRe: My vote of 1memberMarc A. Brown4 May '11 - 9:08 
GeneralRe: My vote of 1memberBill SerGio, Infomercial King4 May '11 - 9:15 
GeneralRe: My vote of 1memberMarc A. Brown4 May '11 - 9:27 
GeneralI am impressed by your intelligent remarksmemberBill SerGio, Infomercial King4 May '11 - 11:50 
GeneralRe: I am impressed by your intelligent remarksmvpPete O'Hanlon4 May '11 - 12:28 
GeneralRe: My vote of 1 [modified]memberEdMan1964 May '11 - 9:50 
GeneralRe: My vote of 1memberBill SerGio, Infomercial King4 May '11 - 10:36 
GeneralRe: My vote of 1memberEdMan1964 May '11 - 10:55 
GeneralRe: My vote of 1memberBill SerGio, Infomercial King4 May '11 - 11:21 
GeneralRe: My vote of 1memberEdMan1964 May '11 - 11:24 
GeneralRe: My vote of 1memberBill SerGio, Infomercial King4 May '11 - 11:51 
GeneralRe: My vote of 1memberOakman4 May '11 - 17:16 
GeneralRe: My vote of 1memberBill SerGio, Infomercial King4 May '11 - 20:15 

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

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