Decorator Design Pattern


Let's begin with a conceptual diagram, the pictorial representation will help us to understand the flow.
Class DecoratorOne is injecting an IComponent interface. This allows you to inject any behaviour you're looking to add, for example, passing the object that you wish to decorate.

So DecoratorOne is acting as a wrapper around decorate. In the above diagram, you can see the Decorator class keeps the reference to the object being decorated. In this case, a component class.
Not only our DecoratorOne class IS-A IComponent but also it HAS-A IComponent. This is because the decorator class implements the same interface as the original Component class, so it now has a chance to intercept any method calls on the interface and inject some additional behaviour into those calls.
Real-life example
  1. Order a pizza: interface Ipizza
  2. Type of pizza (Farmhouse): class FarmHouse: Ipizza
  3. Add topping Mushrooms: class Mushroom: FarmHouse
  4. Add one more topping Sausage: class Sausage: Mushroom
In the end, we ended up in ordering a farmhouse pizza with mushroom & sausages. We kept injecting our new requirement and kept decorating old class to get us something additional.
So when you hear the word decorator, what you're really doing is designing a series of objects that can wrap around each other and inject behaviour as needed.

You can, in fact, define and use multiple decorator objects together so you can have a layered structure around your original object, much like an onion.

Each decorator class can define different behaviours, so you maintain that strong separation of concerns.
Since all of your decorator classes implement the same interface as your original object, you don't have to change any of your client code to add-in these various new behaviours.  
Let's begin with code and implement the following design:
Let's add interface iMobile:
  1. public interface IMobile  
  2.   {  
  3.      double GetPrice();  
  4.      string GetModel();  
  5.   } 
Let's add interface iAndroid:
  1. public interface IAndroid : IMobile  
  2.    {  
  3.        string GetAndroidOS();  
  4.    } 
Now for our default implementation of Android class, let's return some strings & values through our functions.
  1. public class Android : IAndroid  return
  2.     {  we
  3.         public string GetAndroidOS()  
  4.         {  
  5.             return "New Android OS: ";  
  6.         }  
  8.         public string GetModel()  
  9.         {  
  10.             return " New Model: ";  
  11.         }  
  13.         public double GetPrice()  
  14.         {  
  15.             return 999;  
  16.         }  
  17.     } 
At last, on our decorator class, let's add additional information to functions.
  1. public class OnePlus : IAndroid  
  2.    {  
  3.        #region Properties  
  4.        IAndroid Android;  
  5.        #endregion  
  7.        #region Constructor  
  8.        public OnePlus(IAndroid android)  
  9.        {  
  10.            this.Android = android;  
  11.        }  
  12.        #endregion  
  14.        #region overridden methods  
  16.        public string GetAndroidOS()  
  17.        {  
  18.          return  Android.GetAndroidOS() + "Android 10";  
  19.        }  
  21.        public string GetModel()  
  22.        {  
  23.            return Android.GetModel() + "One Plus 8";  
  24.        }  
  26.        public double GetPrice()  
  27.        {  
  28.            return Android.GetPrice() + 54000;  
  29.        }  
  30.        #endregion  
  31.    } 
Now the observer overridden methods are adding additional information to the injected class, hence an IS-A & HAS-A relationship.
Let's go ahead and call it through the program. 
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            IAndroid android= new Android();  
  6.            OnePlus newPhone = new OnePlus(android);  
  7.            Console.WriteLine($"{newPhone.GetModel()}\n {newPhone.GetAndroidOS()} \n Price: {newPhone.GetPrice()}");  
  9.        }  
  10.    } 
 Run the app.
As you can see, it is a combination of both objects. One default implementation & another decorating default object.
Being able to layer objects together in this onion‑type structure and then intercept and modify method calls is a very powerful idea because it allows us to separate concerns and dynamically add new functionality when new needs come up. So, as you work with the decorator pattern, keep these ideas in mind. I hope you gained something out of this article and I wish you good luck. 
Happy Coding!
Connect with me,

Similar Articles