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:

  1. using System;  
  2. using System.Collections;  
  3. using System.Globalization;  
  4. using System.Data.SqlClient;  
  5. using System.Data;   
  6. namespace Test1  
  7. {  
  8.     public class Car  
  9.     {  
  10.         public virtual void CarType()  
  11.         {  
  12.             Console.WriteLine("Simple Car");  
  13.         }  
  14.     }  
  15.     public class WithAC : Car  
  16.     {  
  17.         public override void CarType()  
  18.         {  
  19.             //base.CarType();  
  20.             Console.Write("AC Car");  
  21.         }  
  22.     }  
  23.     public class WithSoundSystemAndAC : WithAC  
  24.     {  
  25.         public override void CarType()  
  26.         {  
  27.             base.CarType();  
  28.             Console.WriteLine("with Sound system");  
  29.         }  
  30.     }   
  31.     class Program  
  32.     {  
  33.         static void Main(string[] args)  
  34.         {  
  35.             Car objCarAC = new WithAC();  
  36.             objCarAC.CarType();  
  37.             Console.WriteLine("\n");   
  38.             Car objCarAll = new WithSoundSystemAndAC();  
  39.             objCarAll.CarType();  
  40.             Console.ReadLine();  
  41.         }  
  42.     }  
  43. }
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.
  1. public class WithSoundSystem:Car  
  2. {  
  3.     public override void CarType()  
  4.     {  
  5.         //base.CarType();  
  6.         Console.Write("Car with only sound system");  
  7.     }  
  8. }
Conclusion: This is the fundamental idea behind the Decorator Design Pattern. I hope you understand.


Similar Articles