Dot Net For All

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

    /// <summary>
    /// This is my legacy component
    /// </summary>
    public interface ICar
    {
        void Drive();
        void Brake();
    }

    public class Car : ICar
    {
        public void Brake()
        {
            Console.WriteLine("Used the pad brakes");           
        }

        public void Drive()
        {
            Console.WriteLine("The speed goes upto 100 Kmph");
        }
    }

    /// <summary>
    /// This class is the decorator of the Car class. It adds and modifies the behaviour of the car class
    /// </summary>
    public class SuperCar:ICar
    {
        public ICar car;
        public SuperCar(ICar car)
        {
            this.car = car;
        }

        public void Brake()
        {
            car.Brake();
            Console.WriteLine("Also has hand brake");
        }

        public void Drive()
        {
            Console.WriteLine("The speed goes upto 300 Kmph");
        }

        public void Music()
        {
            Console.WriteLine("Plays the Hi Fi music system");
        }
    }

    static void Main(string[] args)
    {
        ICar traditionalCar = new Car();
        traditionalCar.Drive();
        traditionalCar.Brake();

        SuperCar superCar = new SuperCar(traditionalCar);
        superCar.Drive();
        superCar.Music();
        superCar.Brake();
        Console.Read();
    }

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.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview