Design Pattern For Beginners - Part 4: Decorator Design Pattern

Welcome to the Design Pattern for Beginners article series. If this is your first article in this series, I recommend you go through my previous articles on the same topic; they are:

This article explains one more very popular structural design pattern called the Decorator Design Pattern. The name itself implies that this is something related to decoration. Yes, in the Decorator Design Pattern we will be implementing one object in through various steps and in each and every step we will add a little feature to it.

Why decorator pattern?


Before beginning the technical discussion and examples we will learn the basics of the Decorator Design Pattern and the primary need for it.

If there is a need to produce one object with many features but not all the features are needed all the time then the Decorator Design Pattern is useful. For example think about a car beautification shop. Where a new car goes to and according to the owner's demand the shop owner beautifies their car.

One owner may like to fit AC into his car where another owner may not like AC, he might instead want a sound system (Hmm, he likes music..) in his new car. Now, the shop owner has all the facilities, and according to car owner's demand he decorates the car. This is where the Decorator Design Pattern can be a solution.

Let's implement our car problem using the Decorator Design Pattern. Have a look at the following code:

using System;
using System.Collections;
using System.Globalization;
using System.Data.SqlClient;
using System.Data; 
namespace Test1
{
    public class
Car
    {
        public virtual void CarType()
        {
            Console.WriteLine("Simple Car");
        }
    }
    public class WithAC :
Car
    {
        public override void CarType()
        {
           
//base.CarType();
            Console.Write("AC Car");
        }
    }
    public class WithSoundSystemAndAC :
WithAC
    {
        public override void CarType()
        {
            base.CarType();
            Console.WriteLine("with Sound system");
        }
    } 
    class
Program
    {
        static void Main(string[] args)
        {
            Car objCarAC = new WithAC();
            objCarAC.CarType();
            Console.WriteLine("\n"); 
            Car objCarAll = new WithSoundSystemAndAC();
            objCarAll.CarType();
            Console.ReadLine();
        }
    }
}

Here, Car is the base class and at first we have derived a class called withAC from the Car class and in the next class we have derived from WithAC classes called WithSoundSystemAndAC.

Now, if anyone needs only AC for their Car then he will be happy by creating an object from the WithAC class. And if you want to fit both an AC and Sound system into your car then just create one object from the WithSoundSystemAndAC class. With this implementation the shop owner can make everyone happy. Here is the sample output.

image1.gif

Now, you may ask the question, what if someone demands only a Sound System in his car but not AC. The solution is derive one class from the Car class where we will implement only a Sound System, no other facility (in other words AC). Here is the simple implementation, the same as the WithAC class.

public class WithSoundSystem:Car
{
    public override void CarType()
    {
       
//base.CarType();
        Console.Write("Car with only sound system");
    }
}


Conclusion: This is the fundamental idea behind the Decorator Design Pattern. I hope you understand.

Up Next
    Ebook Download
    View all
    Learn
    View all