Decorator Pattern in C#

28 May 2021 at 10:00 by ParTech Media - Post a comment

Design patterns in the software world aid in developing faster and tested paradigms. It addresses a futuristic issue which the code (or the developer) will not encounter until it the code is implemented.

In general, design patterns increase code reusability and improve code readability. There are three categories - Creational, Structural and Behavioral.

Creational deals with class instantiation techniques. Structural deals with class object composition and provides ways to add new functionality to a class. Behavioral deals with how the object of the classes communicates.

In this blog, we are going to see a decorator pattern that falls under the Structural category. Let us first see what is decorator pattern and then understand what issue is being addressed with the above mentioned design pattern.

Table of contents

  1. What is decorator pattern?
  2. How to Implement Decorator pattern in C#
  3. Conclusion

What is Decorator Pattern?

Decorator pattern addresses the problem of adding responsibility to the existing classes without affecting the Open Close principle. This is without the need to make any modifications to the existing class dynamically.

It is achieved by adding a wrapper to the existing class. A decorator pattern consists of four main elements -

  1. Component
  2. Concrete component
  3. Decorator
  4. Concrete decorator

The component is the interface which provides operation being consumed by the concrete component. A concrete component is a class that implements the component and has the original operation that needs to be performed. The decorator defines the interface for all the dynamic operations that need to be added to the concrete components. Concrete Decorators are the individual operations that need to be added to the concrete components. It inherits a decorator.

Revolving around the four items, the decorator pattern has been constructed. It is implemented in places where the functionality is being extended without modifying the existing classes.

How to Implement Decorator pattern in C#?

Let us explain this with the help of a real-life situation.

Consider an ice cream shop where a user can customize his/her order. A user has to choose the base flavor of the ice cream and then choose the toppings.

Now, to calculate the total price of the ice cream, we need to add the base price of the ice cream along with the base price of the individual toppings.

We are going to implement this simple scenario using a decorator pattern in C#.

To implement this we are going to use a simple console-based .Net application. We are going to concentrate on implementing the pattern in C# over implementing it particularly in one of the services that can be created using .Net libraries.

Step 1

Create a .Net Core-based console application and provide a valid name and path for the solution.

Step 2

Once the solution is created, open the Program.cs file. This is where the changes are going to be made. The file by default looks like below.

Step 3

As mentioned in the previous section, the idea is to implement the four main elements that are required for the implementation of the decorator pattern. As a first step, we are going to implement the Component. Since it is an ice cream shop, we are creating a component for ice cream.

    public interface Icecream

​    {

​      double CalculatePrice();

​    }

Step 4

Create the concrete component, which is the ice cream flavors in our case. Here, we will consider vanilla and implement the logic for it. The concrete component inherits the component and implements the method CalculatePrice.

   public class VanillaIcecream : Icecream

​    {

​      public double CalculatePrice()

​      {

​        return 15;

​      }

​    }

Step 5

Create the decorator which will be used by the concrete decorator. This is used to add the topping values to the base price of the ice cream. Here, the decorator inherits the component and implements the methods required, and also has a constructor which expects the component to be passed.

   public abstract class ToppingDecorator : Icecream

​    {

​      readonly Icecream _icecream;


​      public ToppingDecorator(Icecream icecream)

​      {

​        _icecream = icecream;

​      }


​      public virtual double CalculatePrice()

​      {

​        return _icecream.CalculatePrice();

​      }

​    }

Step 6

Create the concrete decorator which inherits the ToppingDecorator and overrides the CalculatePrice method. So, that when the updated concrete component or decorator concrete has been passed, the sum value is being calculated.

Below is an example of Wafers which inherits the toppingdecorator and implements the method CalculatePrice, where it holds the logic to add the extra amount along with the base price of the product.

   public class Wafers : ToppingDecorator

​    {

​      public Wafers(Icecream icecream) : base(icecream)

​      {

​      }


​      public override double CalculatePrice()

​      {

​        return base.CalculatePrice() + 3;

​      }

​    }

Similar to wafers, any other toppings can also be added based on the requirement. Below is another example of having choco-chip topping.

   public class Chocochip : ToppingDecorator

​    {

​      public Chocochip(Icecream icecream) : base(icecream)

​      {


​      }


​      public override double CalculatePrice()

​      {

​        return base.CalculatePrice() + 2;

​      }

​    }

Step 7

Now that we have implemented the Component (Icecream interface), Concrete component (VaniallaIceCream), Decorator (ToppingDecorator), and Concrete Decorator (Wafers and Chocochip). Let’s see how to consume them to achieve the decorator pattern.

Open the Program.cs file and include the below lines of code.

var icecreamOrder = new VanillaIcecream();

Console.WriteLine($"Total price of icecream - { icecreamOrder.CalculatePrice() }.");

var wafer = new Wafers(icecreamOrder);

Console.WriteLine($"Total price of icecream with wafer - { wafer.CalculatePrice() }.");

var chocoChip = new Chocochip(wafer);

Console.WriteLine($"Total price of icecream with wafer and chocochip - { chocoChip.CalculatePrice() }.");


In the above example, first, the object of the concrete component (VanillaIcecream) is created. In the next line, the price of the base ice cream is being printed.

Next, an object for the wafer is being created, and the vanillaicecream object is being passed as an input parameter to it. So, the price can be calculated after including the wafer on top of vanilla ice cream.

And if we add a choco-chip on top of the wafer, then the object of the wafer is being passed as an input parameter to the choco-chip class. Then the price is calculated after including the base price of vanilla ice cream, wafer, and choco-chip.

Below is the final data that will be displayed in the console window.


Thanks to Decorator pattern in C#, you are able to achieve additional functionality without modifying the existing classes. This is quite handy for developers as it helps in code reusability and code readability. On top of it, it also helps in achieving the Open-Closed principle.