Using the decorator pattern you can build functionality by stacking objects, which allows objects to dynamically add behaviors to others.
The next diagram is helpful to understand the Decorator pattern:

This is how aggregation stack looks like. The Object A is a base object because it doesn’t contain another object. It will have its own set of behaviors. Object B aggregates Object A allowing Object B augment the behaviors of Object A. We can continue add objects to stack where Object C aggregates Object B and augment behaviors of Object B. The relation is always 1 to 1 in decorator design pattern in order to build up the stack so that one object is on top of another.
To achieve an overall combination of behavior for the stacked objects, you would call upon the top element which is object C, then C -> B -> A. Object A responds with its behavior, then object B adds its incremental behavior and object C adds its incremented behavior.
The actual structure of this design pattern makes use if both interfaces and inheritance so that the classes conform to a common type, whose instances can be stacked up in a compatible way to build up a coherent combination of behavior overall.

Decorator is an abstract class, just like concrete component it implements the IComponent interface. The main differences are that decorator aggregates other types pf components, which will allow us to stack components on top of each other. And decorator servers as the abstract superclass of concrete decorator classes that will each provide an increment of behavior.

In “Coffe” example the ConcreteComponent is a black coffee., the ConcreteDecorator1 can be a milk, the ConcreteDecorator2 can be a sugar. Hence one more benefit from this design pattern – it reduce the number of classes needed to offer a combination of behaviors.
Another example of this patterns is a web page. A basic web page is simply markup using HTML with CSS and JS, however the behavior of web page can be more complex (authorization, search).
%20Class%20Diagram.png?raw=true)
Step 1: Design the component interface
public interface IWebPage {
public void Display();
}
Step 2: Implement the interface with your base concrete component class
public class BasicWebPage: IWebPage {
public string HTML = "...";
public string CSS = "...";
public string Scripts = "...";
}
public void Display() {
// Renders the HTML to the CSS and run embedded scripts
Console.WriteLine("Basic Web Page");
}
Step 3: Implement the interface with your abstract decorator class
public abstract class WebPageDecorator : IWebPage {
protected IWebPage page;
public WebPageDecorator(WebPage webpage) {
this.page = webpage;
]
public void Display() {
this.page.Display();
}
}
Step 4: Inherit from the abstract decorator and implement the component interface with concrete decorator classes
public class AuthorizedWebPage: WebPageDecorator {
public AuthorizedWebPage(WebPage decoratedPage) {
base(decoratedPage);
}
public void AuthorizedUser() {
Console.WriteLine("Authorizing User");
}
public Display() {
base.Display();
this.AuthorizedUser();
}
}
public class AuthenticatedWebPage: WebPageDecorator {
public AuthenticatedWebPage(WebPage decoratedPage) {
base(decoratedPage);
}
public void AuthenticateUser() {
Console.WriteLine("Authenticating User");
}
public Display() {
base.Display();
this.AuthorizedUser();
}
}
Client:
public class Program {
public static void Main(String[] args) {
IWebPage myPage = new BasicWebPage();
myPage = new AuthorizedWebPage(myPage);
myPage = new AuthenticatedWebPage(myPage);
myPage.Display();
}
}
%20Visualization%20Diagram.png?raw=true)
Benefits:
- Add, in effect, any number of behavior dynamically to n object at runtime by using aggregation s a substitute for pure inheritance
- Polymorphism achieved by implementing a single interface.
- Aggregation lets us create a stack of objects.
- Each decorator object in the stack is aggregated in a 1 to 1 relationship with the object below it in stack
- By combining aggregation and polymorphism, we can recursively invoke the same behavior down the stack and have the behavior execute upwards from the concrete component object.
- Reduces the variety of classes you would need to write.
One thought on “Decorator Pattern”