Decorator Pattern in C#

Decorator Pattern in C#

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.

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() }."); Console.ReadLine();

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.

decorator-pattern csharp c#

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Dicey Issues in C/C++

C/C++ problems. If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends.

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look

Simple and practical specification pattern with EF Core and C#

How to use lambda expressions and watch for query performance. Here I address how to implement specification pattern in C#. The key to more efficiency is to use lambda expressions and watch for client evaluation. In this article, I described a hotel booking system and its components. Also, here I explained how the specification pattern improved that architecture. In the current story, I will address how to implement a query handler which utilises this pattern. The framework of choice is EF Core and the language is C#.

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.