Before starting, I would request that you visit the previous articles on Design Patterns series:
- Design Patterns Simplified: Part 1
- Design Patterns Simplified - Part 2 (Singleton)
- Design Patterns Simplified - Part 3 (Simple Factory)
- Design Patterns Simplified - Part 4 (Abstract Factory)
- Design Patterns Simplified - Part 5 (Factory Method)
- Design Patterns Simplified - Part 6 (Prototype)
- Design Patterns Simplified - Part 7 (Builder)
- Design Patterns Simplified - Part 8 (Facade)
- Design Patterns Simplified - Part 9 (Adapter)
I am here to continue the discussion around Design Patterns. Today we will go through one of the structural design pattern called Decorator. Before talking about its implementation let’s begin with defining it. As per GOF guys, Decorator pattern is defined as follows:
“Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclass for extending functionality.”
Well! Let’s understand what they mean.
They mean if we want to dynamically add responsibility to any object, we can go for Decorator pattern instead of sub-classing. Sub-classing has been the usual way to handle any added behavior however it becomes messy and difficult to maintain when dealing with multiple sub-classes. On the other hand, decorator pattern provides easy way to handle not just the multiple sub-classes but also their combinations.
Decorator pattern also enforces “Open-Closed” principle which is one of the SOLID design principle. We will talk about SOLID principle in future articles, however in short, Open-Closed principle suggests that classes should be open to extend and closed to modification.
How Decorator pattern works
Decorator pattern has four key elements as in the following.
Well! Let’s understand what they mean.
They mean if we want to dynamically add responsibility to any object, we can go for Decorator pattern instead of sub-classing. Sub-classing has been the usual way to handle any added behavior however it becomes messy and difficult to maintain when dealing with multiple sub-classes. On the other hand, decorator pattern provides easy way to handle not just the multiple sub-classes but also their combinations.
Decorator pattern also enforces “Open-Closed” principle which is one of the SOLID design principle. We will talk about SOLID principle in future articles, however in short, Open-Closed principle suggests that classes should be open to extend and closed to modification.
How Decorator pattern works
Decorator pattern has four key elements as in the following.
- Component: This is an interface of the object in which we want to add dynamic behavior.
- ConcreateComponent: This is a concreate implementation of Component.
- Decorator: Typically abstract class that defines the dynamic behavior. It also implements Component
- ConcreateDecorator: Concreate implementation of Decorator. Each dynamic functionality implements its own ConcreateDecorator.
We will understand this by simple example.
Let’s start by creating component and take the similar vehicle example we took in previous articles.
- ///<summary>
- /// The component
- ///</summary>
- public interface IVehicle
- {
- string GetBrand();
- string GetModel();
- int GetPrice();
- }
- ///<summary>
- /// The concrete component
- ///</summary>
- public class MarutiCar: IVehicle
- {
- public string GetBrand()
- {
- return "Maruti";
- }
- public string GetModel()
- {
- return "Swift VXI";
- }
- public int GetPrice()
- {
- return 610000;
- }
- }
- ///<summary>
- /// The concrete component
- ///</summary>
- public class HyundaiCar: IVehicle
- {
- public string GetBrand()
- {
- return "Hyundai";
- }
- public string GetModel()
- {
- return "Grand i10 Magna";
- }
- public int GetPrice()
- {
- return 540000;
- }
- }
- ///<summary>
- /// The Decorator abstract class
- ///</summary>
- public abstract class VehicleDecorator: IVehicle
- {
- private IVehicle _vehicle;
- protected VehicleDecorator(IVehicle vehicle)
- {
- _vehicle = vehicle;
- }
- public string GetBrand()
- {
- return _vehicle.GetBrand();
- }
- public string GetModel()
- {
- return _vehicle.GetModel();
- }
- public int GetPrice()
- {
- return _vehicle.GetPrice();
- }
- }
- public class DiwaliOffer: VehicleDecorator
- {
- public DiwaliOffer(IVehicle vehicle): base(vehicle) {}
- public int PercentDiscount = 20;
- public int NewPrice()
- {
- return base.GetPrice() * (100 - PercentDiscount) / 100;
- }
- }
- public class HoliOffer: VehicleDecorator
- {
- publi cHoliOffer(IVehicle vehicle): base(vehicle) {}
- public int PercentDiscount = 15;
- public int NewPrice()
- {
- return base.GetPrice() * (100 - PercentDiscount) / 100;
- }
- }
Setup is ready now. Let’s use them in client and see the action.
- Console.Title = "Decorator pattern demo";
- MarutiCar mCar = new MarutiCar();
- Console.WriteLine("{0} {1} Regular price: {2}", mCar.GetBrand(), mCar.GetModel(), mCar.GetPrice());
- //Diwali Offer
- Diwali OfferdOffer = newDiwaliOffer(mCar);
- Console.WriteLine("{0} {1} Diwali price: {2} after discount of {3} percent", mCar.GetBrand(), mCar.GetModel(), dOffer.NewPrice(), dOffer.PercentDiscount);
- //Holi Offer
- HoliOffer hOffer = new HoliOffer(mCar);
- Console.WriteLine("{0} {1} Holi price: {2} after discount of {3} percent", mCar.GetBrand(), mCar.GetModel(), hOffer.NewPrice(), hOffer.PercentDiscount);

- HyundaiCar mCar2 = new HyundaiCar();
- Console.WriteLine("{0} {1} Regular price: {2}", mCar2.GetBrand(), mCar2.GetModel(), mCar2.GetPrice());
- //Diwali Offer
- DiwaliOffer dOffer2 = new DiwaliOffer(mCar2);
- Console.WriteLine("{0} {1} Diwali price: {2} after discount of {3} percent", mCar2.GetBrand(), mCar2.GetModel(), dOffer2.NewPrice(), dOffer2.PercentDiscount);
- //Holi Offer
- HoliOffer hOffer2 = new HoliOffer(mCar2);
- Console.WriteLine("{0} {1} Holi price: {2} after discount of {3} percent", mCar2.GetBrand(), mCar2.GetModel(), hOffer2.NewPrice(), hOffer2.PercentDiscount);

You can also refer the code in the attached sample.
To summarize, decorator pattern can be used when we want to add dynamic behavior or functionality to the existing objects and also without affecting the bases classes.
Hope you have liked the article. Look forward for your comments/suggestions.
Read more articles on Design Patterns:

Prakash TripathiPosted Apr 11, 2016, 3:44 AM
Thnx Humayun.
Humayun Kabir MamunPosted Apr 11, 2016, 3:43 AM
Nice...
Prakash TripathiPosted Apr 9, 2016, 10:43 AM
Thnx Nagaraj.
Kuppurasu NagarajPosted Apr 9, 2016, 10:38 AM
Nice article..
Prakash TripathiPosted Mar 19, 2016, 11:25 AM
Thnx Bhavik.
Bhavik PatelPosted Mar 19, 2016, 9:24 AM
great share. simple to understant
Prakash TripathiPosted Mar 18, 2016, 10:07 AM
Thnx Pradeep.
Pradeep SahooPosted Mar 18, 2016, 9:52 AM
Nice share ...
Prakash TripathiPosted Mar 18, 2016, 7:21 AM
Thnx Navratna.
Navratna PawalePosted Mar 18, 2016, 7:13 AM
great...
Prakash TripathiPosted Mar 17, 2016, 11:08 PM
Thnx Vignesh.
Prakash TripathiPosted Mar 17, 2016, 11:08 PM
Thnx Sirisha.
Vignesh ManiPosted Mar 17, 2016, 4:56 PM
Nice
Sirisha KPosted Mar 17, 2016, 4:23 PM
Good explanation
Prakash TripathiPosted Mar 17, 2016, 1:55 PM
Thnx Sreenivasa.
sreenivasa kPosted Mar 17, 2016, 1:51 PM
really good one
Prakash TripathiPosted Mar 17, 2016, 11:50 AM
Thnx Sibeesh.
Prakash TripathiPosted Mar 17, 2016, 11:50 AM
Thnx Luis.
Sibeesh VenuPosted Mar 17, 2016, 11:14 AM
Nice Share
Luis CamachoPosted Mar 17, 2016, 9:49 AM
Nice article. :)
Prakash TripathiPosted Mar 17, 2016, 5:53 AM
Welcome Jaipal.
Jaipal ReddyPosted Mar 17, 2016, 4:49 AM
Thank you sir. .
Prakash TripathiPosted Mar 17, 2016, 4:09 AM
Thnx Ibrahim.
Mohammed IbrahimPosted Mar 17, 2016, 4:04 AM
nice