Introduction
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
- Order a pizza: interface Ipizza
- Type of pizza (Farmhouse): class FarmHouse: Ipizza
- Add topping Mushrooms: class Mushroom: FarmHouse
- 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:
- public interface IMobile
- {
- double GetPrice();
- string GetModel();
- }
Let's add interface iAndroid:
- public interface IAndroid : IMobile
- {
- string GetAndroidOS();
- }
Now for our default implementation of Android class, let's return some strings & values through our functions.
- public class Android : IAndroid return
- { we
- public string GetAndroidOS()
- {
- return "New Android OS: ";
- }
- public string GetModel()
- {
- return " New Model: ";
- }
- public double GetPrice()
- {
- return 999;
- }
- }


Join the conversation! Your thoughts help the community grow.