Monday, January 08, 2007

Implementing Decorator Pattern in Java

In the Decorator pattern, a decorator object is wrapped around the original object. This is typically achieved having the original object as a member of the decorator, with the decorator forwarding the requests to the original object and also implementing the new functionality. The decorator must conform to the interface of the original object (the object being decorated). In Design Patterns, the authors define the Decorator pattern as:
Attach additional responsibilities to an object dynamically. Decorators
provide a flexible alternative to subclassing for extending functionality.
Skip to Sample Code

Usage Scenarios
  • Add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.
  • Be able to withdraw responsibilities
    For example: The java.util.Collections.unmodifiableCollection(Collection) removies the ability to change a given collection by wrapping it with a decorator that throws an UnSupportedException when you try to modify the Collection.
  • When extension by subclassing is impractical, such as when a large number of independent extensions produce an explosion of subclasses to support every combination. Or a class is unavailable for subclassing.
Here is the UML diagram of the Decorator pattern followed by a description of the various involved components.
  • Component: Defines the interface for objects that can have responsibilities added to them dynamically.
  • ConcreteComponent: Defines an object to which additional responsibilities can be attached.
  • Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.
  • ConcreteDecorator: adds responsibilities to the component.
Implementing Decorator Pattern
The following is sample implementation of the Decorator pattern in Java.
  1. The Component Interface
    package decorator;

    public interface IComponent {
    public void doStuff();
  2. The Concrete Component
    package decorator;

    public class Component implements IComponent{
    public void doStuff() {
    System.out.println("Do Suff");

  3. The Decorator
    package decorator;

    public interface Decorator extends IComponent {
    public void addedBehavior();
    Note: The decorator interface has to conform to the component interface, hence it extends IComponent.
  4. The Concrete Decorator
    package decorator;

    public class ConcreteDecorator implements Decorator {

    IComponent component;

    public ConcreteDecorator(IComponent component) {
    this.component = component;

    public void addedBehavior() {
    System.out.println("Decorator does some stuff too");


    public void doStuff() {


  5. The Client

    import decorator.*;

    public class Client {
    public static void main(String[] args) {
    IComponent comp = new Component();
    Decorator decorator = new ConcreteDecorator(comp);



  1. Man you are a rock star!! Extremely simple explanations, especially for little brained ones like me :-)

  2. u have not defined the ref to component object in the decorator interface......but instead defined it in concretedecorator.....

  3. Hi,you simply rock.

    I would expect a bit more from you, like using one more subclass for decorator

    Anyways Good job bro,


Popular Posts