Design Patterns in C# Part 3 - Enhancing Functionality With The Decorator Pattern - by João Melo - Medium
Design Patterns in C# Part 3 - Enhancing Functionality With The Decorator Pattern - by João Melo - Medium
Open in app
Search
Introduction
Welcome back to the Ultimate Guide to Design Patterns in C#! In this series, we
explore various design patterns that can significantly enhance your software design
and development. In the first two parts, we unleashed the power of the Strategy
Pattern and simplified object creation with the Factory Method Pattern. Today, we’ll
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-3-enhancing-functionality-with-the-decorator-pattern-4750343e6f63 1/13
19/12/2024, 16:05 Design Patterns in C# Part 3: Enhancing Functionality with the Decorator Pattern | by João Melo | Medium
dive into the Decorator Pattern and see how it can dynamically add behavior to
objects.
💡Type — Structural patterns deal with object composition or how classes and
objects can be combined to form larger structures. These patterns help ensure that
if one part of a system changes, the entire system doesn’t need to change.
The Decorator Pattern is particularly useful when you want to add responsibilities to
objects dynamically and transparently, without affecting other objects. It’s also
beneficial when extending behavior using inheritance is impractical due to a large
number of independent extensions or when you need to add responsibilities to an
object that you may not know about at compile time.
Imagine we are building a coffee shop application where customers can order
different types of coffee and customize their drinks with various condiments like
milk, sugar, and whipped cream. Instead of creating a class for each possible
combination of coffee and condiments, we can use the Decorator Pattern to add
condiments dynamically to the coffee.
and GetCost . This interface will be implemented by both the base coffee class and
the decorators, ensuring they all provide a description and cost of the coffee.
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-3-enhancing-functionality-with-the-decorator-pattern-4750343e6f63 2/13
19/12/2024, 16:05 Design Patterns in C# Part 3: Enhancing Functionality with the Decorator Pattern | by João Melo | Medium
double GetCost();
}
Next, we create the SimpleCoffee class that implements the ICoffee interface. This
class provides the basic functionality for a simple coffee, returning a description
and the base cost of the coffee.
We then define an abstract class CoffeeDecorator that also implements the ICoffee
interface. This class holds a reference to an ICoffee object and delegates the
GetDescription and GetCost methods to the wrapped object. It serves as the base
class for all concrete decorators.
and WhipDecorator that extend CoffeeDecorator . Each of these classes adds its own
behavior to the GetDescription and GetCost methods, augmenting the wrapped
coffee object with additional properties and cost.
Finally, in the client code, we demonstrate how to use the decorators to dynamically
add behavior to the coffee object. We start with a SimpleCoffee and wrap it with
various decorators to add milk, sugar, and whipped cream, displaying the updated
description and cost after each addition. This illustrates the flexibility and
extensibility provided by the Decorator Pattern.
class Program
{
static void Main(string[] args)
{
ICoffee coffee = new SimpleCoffee();
Console.WriteLine($"{coffee.GetDescription()} : ${coffee.GetCost()}");
Conclusion
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-3-enhancing-functionality-with-the-decorator-pattern-4750343e6f63 5/13
19/12/2024, 16:05 Design Patterns in C# Part 3: Enhancing Functionality with the Decorator Pattern | by João Melo | Medium
In this post, we’ve explored the Decorator Pattern, a powerful tool for dynamically
adding behavior to objects without modifying their structure. By using decorator
classes to wrap concrete components, we can enhance the functionality of objects in
a flexible and reusable manner. We’ve demonstrated this with a coffee shop
example, where customers can customize their coffee with various condiments like
milk, sugar, and whipped cream.
In the next post of this series, we will explore the Observer Pattern and discover
how it can further improve your code’s flexibility and maintainability.
Backend Development
Follow
Software engineer, specializing in backend development with the C# language and the .NET Core.
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-3-enhancing-functionality-with-the-decorator-pattern-4750343e6f63 6/13