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:
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.


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