Sitemap

Decorator Design Pattern

2 min readJul 5, 2023

--

We have many design patterns. Currently, we will discuss Decorator.

Details

The Decorator Pattern is a structural design pattern that allows behavior to be added to an individual object dynamically, without affecting the behavior of other objects from the same class. It provides a flexible alternative to subclassing for extending the functionality of an object at runtime.

To understand the Decorator Pattern, let’s consider an example of a text editor application that allows users to write and format text. The application provides a Text interface and a concrete implementation called PlainText.

Define the Text Interface:

public interface Text {
String getContent();
}

Implement the Concrete Component:

public class PlainText implements Text {
private String content;
public PlainText(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}

Create a Decorator:

public abstract class TextDecorator implements Text {
protected Text decoratedText;
public TextDecorator(Text decoratedText) {
this.decoratedText = decoratedText;
}
public String getContent() {
return decoratedText.getContent();
}
}

Implement Concrete Decorators:

public class BoldTextDecorator extends TextDecorator {
public BoldTextDecorator(Text decoratedText) {
super(decoratedText);
}
public String getContent() {
return "<b>" + super.getContent() + "</b>";
}
}
public class ItalicTextDecorator extends TextDecorator {
public ItalicTextDecorator(Text decoratedText) {
super(decoratedText);
}
public String getContent() {
return "<i>" + super.getContent() + "</i>";
}
}

In this example, the TextDecorator class acts as the base decorator, while BoldTextDecorator and ItalicTextDecorator are the concrete decorators that add specific formatting to the text.

Get Amit Dhiman’s stories in your inbox

Join Medium for free to get updates from this writer.

Now, let’s see the usage of decorators:

Text plainText = new PlainText("Hello, world!");
Text boldText = new BoldTextDecorator(plainText);
System.out.println(boldText.getContent()); // Outputs: <b>Hello, world!</b>
Text italicText = new ItalicTextDecorator(boldText);
System.out.println(italicText.getContent()); // Outputs: <i><b>Hello, world!</b></i>

In this example, we start with a plain text object (PlainText) and then wrap it with decorators (BoldTextDecorator and ItalicTextDecorator) to add formatting to the text. Each decorator modifies the behavior of the decorated text by adding its specific formatting tags.

The Decorator Pattern allows us to add multiple decorators in any order and combine their effects dynamically. It provides a flexible way to extend the behavior of objects without relying on inheritance or modifying the original classes.

The key benefits of the Decorator Pattern include the ability to add functionality to objects at runtime, maintain open-closed principle, and achieve fine-grained control over object behavior by combining decorators in different ways.

--

--

Amit Dhiman
Amit Dhiman

Written by Amit Dhiman

Sr. Software Developer, Frontend developer with 9+ years of experience. Specializing in Kotlin, Java, Flutter, Angular, Javascript, Typescript, AWS