Recommended sites

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits
Decorator Print E-mail
User Rating: / 1
PoorBest 
Thursday, 20 January 2005 21:19
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.Component#doSomeStuff() */
	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
Add New Search RSS
Write comment
Name:
Email:
 
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
:):grin;)8):p:roll:eek:upset:zzz:sigh:?:cry
:(:x
Please input the anti-spam code that you can read in the image.

3.20 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Saturday, 18 June 2005 00:10 )
 


Another articles:


Content View Hits : 2417111

Enter Amount: