When and where to use C# Decorator Pattern

In this article I will discuss one of the important and structural design pattern. The pattern I want to discuss is the decorator pattern. I will use the C# code example to illustrate the use and implementation of this pattern.

Where the decorator pattern is used

Since the decorator pattern is structural pattern. And these patterns are mainly used in the scenarios where we have already developed the software and we are in the design or in the maintenance or extension of the development life cycle.

Structural pattern is implemented where we have not foreseen the changes while development of the classes. It means to change the state or behavior of the existing class can be a costly state of affair. Or if we are working on some legacy class which cannot be modified now as modification can effect the working of the class badly. Or the component class is not available for sub classing.

All in one I can say is that the role of the decorator pattern is to add the state and behavior to an object dynamically. The object does not know that it is being decorated therefore it is useful pattern for evolving systems.

While implementing decorator we should keep one important point in mind that the Decorator should inherit from the legacy class or interface and should also contain the object of the same class which should be set up while instancing the decorator.

Decorator example

The above is the sample code of example for potential decorator usage. In the above code I have Component interface (ICar) which has a concrete implementation(Car). This is the perfectly working and tested component. We should not change this system.

Now a need comes that we have to develop a super car which should work on the basis of normal car. In this case I have created decorator(SuperCar). This class I have inherited from the component interface as well as it also contains an property of the same component.

Now I have added the new Added a new functionality of Music. Changed the Drive speed to some new value(300 Kmph) and modified the existing braking capability to have one more braking option(“hank brake”);

Example from C#

Decorator pattern is used by C# language itself. It is used to to decorate the Stream I/O class of C#. The decorated versions are BufferedStream, FileStrem, MemoryStrem, NetworkStream and CryptoStream classed.

These subclasses inherit from the Stream class and also contain an instance of the Stream class.

Conclusion

In this article I have discussed one of the important and basic structural pattern with C# example.

Vikram Chaudhary

I have 8 years of experience in developing .NET applications. I have worked on both web as well as windows based application using ASP.NET and WPF. I have keen interest in learning and implementing new things. You can always get in touch with me at vikram.chaudhary@dotnetforall.com or vikram.vee@outlook.com. Please let me know your thoughts about this article
  • SanMissar

    Shouldn’t there be an abstract SuperCar from which the concrete SuperCar be derived?

    • Hello Sir, It is generally not recommended to have multilevel decorators and I am not able to think of a scenario where it should be useful.

      • SanMissar

        Well, if you see the Gang of Four version, the structure would go like

        Component
        |
        —————————-
        | |
        Concrete Decorator
        Component |
        ConcreteDecorator

        May be, I am being too literal 🙂

  • Dmitry Bytchenko

    Probably, the most evident decorator pattern using is Linq: having IEnumerable we return the same IEnumerable (please, notice, that T1 is not necessary T2) but decorated: var result = File.ReadLines(@"C:MyFile.csv").Skip(1).Select(line => line.Split(',')).Select(items => int.Parse(items[1])).Where(item => item > 0).OrderBy(item => item).ToArray();