Labels:
example/sample code,
patterns
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:
Usage Scenarios
Here is the UML diagram of the Decorator pattern followed by a description of the various involved components.
The following is sample implementation of the Decorator pattern in Java.
Attach additional responsibilities to an object dynamically. DecoratorsSkip to Sample Code
provide a flexible alternative to subclassing for extending functionality.
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.
The following is sample implementation of the Decorator pattern in Java.
- The Component Interface
package decorator;
public interface IComponent {
public void doStuff();
}IComponent.java - The Concrete Component
package decorator;
public class Component implements IComponent{
public void doStuff() {
System.out.println("Do Suff");
}
}Component.java - The Decorator
package decorator;
public interface Decorator extends IComponent {
public void addedBehavior();
}Decorator.java Note: The decorator interface has to conform to the component interface, hence it extends IComponent. - The Concrete Decorator
package decorator;
public class ConcreteDecorator implements Decorator {
IComponent component;
public ConcreteDecorator(IComponent component) {
super();
this.component = component;
}
public void addedBehavior() {
System.out.println("Decorator does some stuff too");
}
public void doStuff() {
component.doStuff();
addedBehavior();
}
}ConcreteDecorator.java - The Client
import decorator.*;
public class Client {
public static void main(String[] args) {
IComponent comp = new Component();
Decorator decorator = new ConcreteDecorator(comp);
decorator.doStuff();
}
}Client.java

2 comments:
Man you are a rock star!! Extremely simple explanations, especially for little brained ones like me :-)
u have not defined the ref to component object in the decorator interface......but instead defined it in concretedecorator.....
Post a Comment