The Decorator pattern extends an object's behavior at runtime by wrapping it in one or more decorator objects that implement the same interface as the original. This gives flexible, composable behavior without relying on subclassing.
A text editor might wrap a plain TextView with a SpellCheckDecorator and a HighlightDecorator, letting you enable or disable each feature per instance simply by choosing which decorators to apply, instead of creating a subclass for every combination.
public class TextViewDecorators {
interface TextView { String render(String text); }
static class PlainTextView implements TextView {
public String render(String text) { return text; }
}
static class SpellCheckDecorator implements TextView {
private final TextView inner;
SpellCheckDecorator(TextView inner) { this.inner = inner; }
public String render(String text) {
return inner.render(text.replace("recieve", "~recieve~")); // mark the typo
}
}
static class HighlightDecorator implements TextView {
private final TextView inner;
private final String word;
HighlightDecorator(TextView inner, String word) { this.inner = inner; this.word = word; }
public String render(String text) {
return inner.render(text).replace(word, "**" + word + "**");
}
}
public static void main(String[] args) {
String text = "Please recieve the Java notes";
TextView basic = new PlainTextView();
TextView spell = new SpellCheckDecorator(basic);
TextView both = new HighlightDecorator(spell, "Java");
System.out.println(basic.render(text));
System.out.println(spell.render(text));
System.out.println(both.render(text));
}
}Please recieve the Java notes
Please ~recieve~ the Java notes
Please ~recieve~ the **Java** notes
A concise interview answer is
Decorator promotes flexibility by letting you wrap an object with additional behavior at runtime, and because decorators share the same interface as the original object, you can stack several of them in any combination without creating a new subclass for every feature combination.
Last updated: 1 November 2026
Previous question: Can you provide a real-world example of the Decorator pattern?
Next question: What is the Facade pattern and how does it simplify interactions with complex systems?