Exploring the Decorator Pattern

Having learned about the Adapter and Composite Patterns, you're well on your way to mastering structural design patterns in C#. In this lesson, we'll dive into the Decorator Pattern, another important structural pattern that allows us to add new functionalities to objects dynamically and transparently. This is particularly useful when you want to enhance the behavior of objects without modifying their code.

The Decorator Pattern enables you to wrap an object with additional behavior in a flexible and reusable way. You'll learn how to use this pattern to extend the functionality of objects in a clean and maintainable manner.

Understanding the Decorator Pattern

To understand the Decorator Pattern, let's think about a coffee shop where you can customize your coffee with various add-ons. Consider starting with a simple cup of coffee and then enhancing it with ingredients like milk, sugar, or whipped cream.

Simple Coffee (Core Component): Think of the basic coffee as the core component. It has fundamental properties, such as description and cost.

Milk (Decorator): Adding milk to the coffee decorates it with extra description ("Milk") and additional cost.

Sugar (Decorator): Similarly, adding sugar will decorate the coffee with a sugar description and additional cost.

Multiple Decorations

You can layer decorators. For instance, you can first add milk and then sugar to the already milk-decorated coffee. Each decorator wraps the core component or another decorator, adding its behavior.

In this structure, each decorator extends the functionality of the original component dynamically. You start with a basic coffee and, by wrapping it with different decorators, you can create complex coffee orders.

Benefits of the Decorator Pattern

The Decorator Pattern allows you to dynamically add or remove functionality to objects without altering their structure. This leads to more modular and maintainable code compared to subclassing for every possible combination of enhancements. In our coffee shop example, you can easily extend the coffee's behavior at runtime by adding decorators like milk, sugar, or any other ingredients.

Step 1: Creating the Coffee Class

We start by defining an abstract Coffee class. This class includes a GetDescription method and a Cost method that will be overridden by subclasses. The SimpleCoffee class extends Coffee and provides the implementation for the Cost method.

Step 2: Creating the CoffeeDecorator Base Class

Next, we create a CoffeeDecorator class that also extends Coffee. This class takes an instance of Coffee and delegates calls to GetDescription and Cost methods to the wrapped instance.

Step 3: Adding MilkDecorator

Following this, we extend CoffeeDecorator to create a MilkDecorator class. It overrides the GetDescription and Cost methods to add the description and cost of milk to the original coffee.

Step 4: Adding SugarDecorator

Similarly, we create a SugarDecorator class by extending CoffeeDecorator. This decorator adds sugar-related description and cost.

Applying the Decorator Pattern

Now, let's see how these components work together in the main method:

  1. Creating a SimpleCoffee instance: We start by creating a basic SimpleCoffee object. This represents our core component with the base functionality.

  2. Decorating with Milk: Next, we wrap (decorate) the SimpleCoffee object with a MilkDecorator. This adds the milk description and cost to the original coffee.

  3. Decorating with Sugar: Finally, we wrap the milk-decorated coffee with a SugarDecorator. This adds the sugar description and cost to the already milk-enhanced coffee.

This demonstrates how you can start with a base component (SimpleCoffee) and dynamically add functionalities by layering decorators. Each decorator adds its own behavior while preserving the core functionality of the base component.

Conclusion

Mastering the Decorator Pattern is important because it allows you to extend the functionality of objects at runtime without altering their structure. This leads to more flexible and maintainable code compared to using subclassing for every new feature. In our coffee example, you can easily add new features like milk and sugar by layering decorators, which can be combined in various ways. Whether you need to add functionality to user interface components, logging systems, or graphical rendering engines, the Decorator Pattern provides a powerful and elegant solution.

Let's get started with the practice section, where you'll implement and extend the Decorator Pattern step-by-step.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal