Decorator Pattern in C#: Full Guide

Aug 16, 2023 | C#, .NET

Hold onto your hats folks, because we’re about to embark on an enlightening journey through the world of Decorator Pattern in C#. As a seasoned C# developer with a penchant for patterns, I’m stoked to share this adventure with you.

So what’s the game plan? We’ll kick things off with the basics, jump headfirst into method decorators, class decorators, and even throw in some real-world examples for good measure. Let’s get cracking!

Decorator Pattern in C#

In the programming language C#, we often hear phrases such as “Wrapper” or “Decorator”. These terms sometimes feel like random buzzwords flying around. But today, we pause to demystify the true essence of the Decorator Pattern in C# — an intricate architectural design that has been influential to developers around the globe.

Understanding the Decorator Design Pattern in C#

Let’s kick-start by understanding what the Decorator Design Pattern C# is all about. The decorator pattern is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.

The truth is, much of coding is about how to best manage complexity. And this pattern can be a lifesaver when you need to add responsibilities to an object on the go.

Here’s a simple example to illustrate how the decorator pattern works in C#. For didactic purposes, I have kept this example quite basic. But note that real-world applications of this pattern often come into play in more complex scenarios.

// Defining the basic interface or base class
public interface Component {
    void Operation();
}

// Creating concrete components
public class ConcreteComponent : Component {
    public void Operation()
    {
        Console.WriteLine("ConcreteComponent: Firing operation...");
    }
}

// Creating the base decorator, although it's not mandatory
public abstract class Decorator : Component {
    protected Component _component;

    public void SetComponent(Component component)
    {
        this._component = component;
    }

    public void Operation()
    {
        if (_component != null)
        {
            _component.Operation();
        }
    }
}

// Creating the concrete decorator
public class ConcreteDecoratorA : Decorator {
    public override void Operation()
    {
        base.Operation();
        Console.WriteLine("ConcreteDecoratorA: Extended operation executed...");
    }
}

In this example, the ConcreteComponent is a primary entity, having its core logic. It implements the Component interface, which works as an outline of the patterns orchestrating the entire operation. The Decorator is an abstract class implementing Component interface too. It acts as the base wrapper or decorator around the core components.

The ConcreteDecoratorA is tasked with adding new behavior to the ConcreteComponent. In our example, it just adds a message to the console, but imagine all the possible eloquent functional extensions you could add!

Practical Applications of the Decorator Pattern in C#

A real-world example where you might find this pattern beneficial is when dealing with visual components in a user interface. You might have a basic TextComponent class, which has basic text features including setting text, getting text, and so on. Yet, you might want to enrich the text with features such as Bold, Italics, and Underline.

// concrete component
public class TextComponent : Component {
  public string Text { get; set; }

  public void Operation() {
    Console.WriteLine($"{Text}");
  }
}

// concrete decorator
public class BoldDecorator : Decorator {
  public override void Operation() {
    base.Operation();
    Console.WriteLine("Rendered in bold");
  }
}


// concrete decorator
public class ItalicDecorator : Decorator {
  public override void Operation() {
    base.Operation();
    Console.WriteLine("Rendered in italic");
  }
}

In this instance, you could use the Decorator Design Pattern to add these different behaviours (Bold, Italic) to a TextComponent object. Each decorator (BoldDecorator, ItalicDecorator) would extend the TextComponent‘s base functionality without modifying any other objects of the TextComponent class.

The Decorator Design Pattern offers a sound solution to maintain the class code DRY (Don’t Repeat Yourself), enhancing flexibility and ensuring the code is open for extension but closed for modification — adhering to the Open-Closed Principle. It’s also a worthy partner when you need to add functionalities dynamically without tampering with the sweet simplicity of single-responsibility classes.

Thus, the Decorator Pattern in C# comes as an eloquent coding design, offering coders a smart way to sprinkle additional functionalities to objects at runtime, without bloating the core code with extended features. So the next time your decoders spin around the “Wrapper” or “Decorator”, you’d confidently know what it implies — and more importantly, where and how you could spin it in your own code.

In-depth Exploration of Decorator Pattern C# with Code Examples

Understanding coding patterns and their underlying concepts can often be challenging. However, concrete, hands-on examples can make these abstract concepts more digestible and approachable.

In this section, we’ll delve more deeply into Decorator Pattern C#, analyze a practical example, and demonstrate how the pattern operates and enhances your object-oriented programming.

Deciphering the C# Method Decorator

Method decorators in C# can sometimes send developers into a minor panic due to their perceived complexity. While it is true that they can seem daunting at first, once understood, they can be incredibly powerful tools in your C# toolbox, offering ways to elegantly add additional functionality to methods without significant modification to the underlying logic.

In its simplest form, a C# method decorator is a custom attribute that you apply to a method, altering its behavior. Let’s consider the following example.

// custom attribute that acts as a method decorator
public class LogExecutionTime : Attribute, IMethodDecorator
{
    private Stopwatch timer;

    public void OnEntry(MethodExecutionArgs args)
    {
        timer = Stopwatch.StartNew();
    }

    public void OnExit(MethodExecutionArgs args)
    {
        timer.Stop();
        Console.WriteLine($"Execution Time: {timer.ElapsedMilliseconds} ms");
    }
}

// usage
[LogExecutionTime]
public void MyMethod()
{
    // method logic here...
}

In this example, LogExecutionTime serves as a method decorator. As the name implies, this decorator logs the total execution time of MyMethod – it starts a stopwatch when the method is entered and stops it when the method ends, logging the elapsed time. This can offer valuable insights into the performance of your methods, particularly if you’re diagnosing latency issues.

// other operation
[LogExecutionTime]
public void MyMethod2()
{
    // Another method logic
}

What’s more, one of the greatest strengths of the decorator pattern is that once you’ve defined a decorator, it can be reused across multiple methods. In the example above, LogExecutionTime can also be applied to MyMethod2.

Applying this decorator helps ensure that your runtime diagnostics are consistent across the methods in your codebase.

Decorator Pattern in C#: Real World Scenarios

The decorator pattern in C# offers a versatile framework to include additional functionalities to an object, and what’s more, it does so dynamically at runtime. To truly highlight the practical implications and uses of this strategy, we are going to proceed with more real-world examples.

Expanding on C# Class Decorators

While the industry is increasingly moving toward writing concise, effective, and clean code – or as developers commonly refer to it, DRY (Don’t Repeat Yourself) code – C# Class Decorators stand out as a potent tool in achieving these benchmarks. Beyond enhancing code aesthetics, class decorators tangibly improve code quality and maintainability.

Consider an ASP.NET Core project, where the route to an MVC action must be serialized in a specific format. Class decorators, in this case, come in handy to specify a custom routing.

// Define a custom route attribute as a class decorator
[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

In this example, the Route attribute, a standard class decorator in ASP.NET Core, ensures that the URL leading to any action in the ValuesController would follow the pattern ‘api/[controller name]’. In addition to improving code clarity, this classifier helps maintain a unified, predictable routing structure.

Practical Utilization of C# Method Decorators

Alongside class decorators, C# Method Decorators are another matchless instrument that facilitate functionality extension at the individual method level.

Using the PostSharp library, we can implement a caching mechanism for a method:

[Cache]  // Method decorator from PostSharp library
public static string GetData()
{
    // Expensive database or API operation...
    return expensiveData;
}

This code snippet employs the Cache attribute from the PostSharp library as a method decorator. Each time GetData() is invoked, the Cache ensures that, if possible, data is fetched from cache storage instead of executing an expensive operation again. By reducing the frequency of expensive operations, the decorator improves the application’s performance significantly.

Real-World Use Cases: File Reading and Writing

Believe it or not, the decorator pattern in C# makes reading and writing to a file in .NET remarkably efficient and streamlined.

Did you know that StreamReader and StreamWriter classes, hailed for their convenience and functionality, are practical examples of decorator pattern in C#? Let’s analyze a chunk of code that uses these classes:

// Example of decorator pattern in file reading and writing in C#
using (var stream = File.OpenRead("example.txt"))
using (var reader = new StreamReader(stream))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

Both StreamReader and StreamWriter implement the TextReader and TextWriter abstract classes, respectively. They wrap a Stream object, and provide additional functionality (e.g., reading from and writing to a stream). stream is the standard File.OpenRead object stream, and new StreamReader(stream) decorates or ‘wraps’ this stream, allowing for reading line by line.

With these real-world examples at your fingertips, the decorator pattern’s practicality and its impact on improving code structure should now become evident. Its appeal lies in how effortlessly it can add or alter behaviour in a fluent, logical, and maintainable manner. Indeed, once you’ve tasted the potency and flexibility of the decorator pattern and class/method decorators, there’s no turning back.

The Practicality and Power of C# Decorators

As we journey through the final lap, let’s see the broader picture. Decorator Pattern in C# isn’t just a flashy term; it’s got real teeth and delivers measurable impacts. The usage of decorators breathes life into your code, enhancing readability and reducing future headaches. Here’s the deal: get cooking with C# decorators, and I’ll bet you’ll wonder how you ever managed without them!

Uh oh, we’re nearing the end of our journey. But remember, in the world of coding, there’s no final destination, only exciting new pathways. Keep exploring the realm of patterns and decorators, and your coding future could look quite bright! So, what’s it gonna be: will you treat decorator patterns as your helpful sidekick or let their latent power remain untapped?

You May Also Like

Sign up For Our Newsletter

Weekly .NET Capsules: Short reads for busy devs.

  • NLatest .NET tips and tricks
  • NQuick 5-minute reads
  • NPractical code snippets
.