Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.



Source Code

/**
 * Defines the interface for objects that can have responsibilities added to the
 * dynamically.
 * 
 * @role __Component
 */
public abstract class Component {
	/**
	 * Sample operation.
	 */
	public abstract void doSomeStuff();
}

/**
 * Defines an object to which additional responsibilities can be attached.
 */

public class ConcreteComponent extends Component {
	/** @see patterns.gof.decorator.ComponentdoSomeStuff() */
	public void doSomeStuff() {
		// provide implementation here
	}
}

/**
 * Adds responsibilities to the component.
 */

public class ConcreteDecorator extends Decorator {
	public ConcreteDecorator(Component decorateMe) {
		super(decorateMe);
	}

	/**
	 * Behavior added by decorator.
	 */
	private void addedBehavior() {
		// some extra functionality goes here
	}

	public void doSomeStuff() {
		super.doSomeStuff();
		addedBehavior();
	}
}

/**
 * Maintains the reference to a Component object and defines an interface that
 * conforms to Component's interface
 * 
 * @role __Decorator
 */

public abstract class Decorator extends Component {
	/** reference to the decorated component */
	protected Component component;

	/**
	 * @param decorateMe
	 *            component to decorate
	 */
	public Decorator(Component decorateMe) {
		this.component = decorateMe;
	}

	public void doSomeStuff() {
		component.doSomeStuff();
	}
}
comments powered by Disqus

You might like also

Proxy
Provide a surrogate or placeholder for another object to control access to it. Options are provided to implement all interfaces of the subject class as well as all of the public methods of the subject classSource Code/** * Represents a proxy for Subject */ public class Proxy extends Subject } /** * Represents a real subject */ public class RealSubject extends Subject } /** * Represents a subject * * @role __Subject */ public abstract class Subject …
5294 Days ago
Flyweight
Use sharing to support large numbers of fine-grained objects efficiently.Source Code/** * Declares an interface through which flyweights can receive and act on * extrinsic state. * * @role __Flyweight */ public interface Flyweight /** * Represents extrinsic state of flyweight(s). * * @role __State */ public interface FlyweightContext /** * Implements the Flyweight interface and adds storage for intrinsic state, if * any. Objects of this class must be sharable. Any state it stores must be * intrinsic (independent …
5294 Days ago
Adapter
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.Source Code/** * Defines an existing interface that needs adapting * * @role __Adaptee */ public class Adaptee } /** * This class adapts the interface of Adaptee to the Target interface */ public class Adapter extends Target /** * Implementation of target method that uses adaptee to perform task */ public void request() } /** …
5294 Days ago
Bridge
Decouple an abstraction from its implementation so that the two can vary independently.from the gang of the fourSource Code/** * Defines Abstraction interface. Stores reference to implementation. * * @role __Abstraction */ public abstract class Abstraction /** * This sample operation delegates call to particular implementation */ public void someOperation() } /** * Concrete implementation */ public class ConcreteImplementorA extends Implementor } /** * Concrete implementation */ public class ConcreteImplementorB extends Implementor } /** * Defines interface for …
5294 Days ago