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();
IComponent b = new PeanutTopping(a);
IComponent c = new CandyTopping(b);
c.AddTopping();
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(); }
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; }
void IComponent.AddTopping()
{
}
}
public class CandyTopping : Topping, IComponent
{
public CandyTopping(IComponent i)
: base(i)
{
}
void IComponent.AddTopping()
{
input.AddTopping(); Console.WriteLine("Candy Topping added");
}
}
public class PeanutTopping : Topping, IComponent
{
public PeanutTopping(IComponent i)
: base(i)
{
}
void IComponent.AddTopping()
{
input.AddTopping(); Console.WriteLine("Peanut Topping added");
}
}
public class NutsTopping : Topping, IComponent
{
public NutsTopping(IComponent i)
: base(i)
{
}
void IComponent.AddTopping()
{
input.AddTopping(); Console.WriteLine("Nuts Topping added");
}
}

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