[keywords]:java,design pattern, design pattern, Java and pattern learning, decorator, decoration mode
[Environment]:staruml5.0 + JDK6
[Author]:winty (wintys@gmail.com)
[Body]:
package pattern.decorator; /** * Decoration Mode: Decorator pattern * @version 2009-6-5 * @author winty (wintys@gmail.com)/public class Decoratortest
{public static void main (string[] args) {Component Component;
component = new Concretecomponent ();
Component.dosomething ();
System.out.println ("");
Transparent decorative mode Component decorator;
Decorator = new Concretedecorator (component);
Decorator.dosomething ();
System.out.println ("");
Translucent decorative pattern concretedecorator decorator2;
Decorator2 = new Concretedecorator (component);
Decorator2.dosomething ();
Decorator2.dootherthings ();
}/** * Abstract widget: Component * * Interface component{public abstract void dosomething (); /** * Concrete Widget: Concretecomponent/class Concretecomponent implements component{@Override public void Doso
Mething () {System.out.println ("do something");
} } /** * Decoration: Decorator */abstract class Decorator implements component{private Component comp;
Public decorator (Component comp) {this.comp = comp;
@Override public void DoSomething () {comp.dosomething (); }/** * Specific decoration: Concretedecorator */class Concretedecorator extends decorator{public concretedecorator (Comp
Onent comp) {super (comp);
@Override public void DoSomething () {super.dosomething ();
Domorethings ();
private void Domorethings () {System.out.println ("do more things.");
/** * The new method is not part of the component interface, * so it cannot be accessed transparently through the component interface.
*/public void dootherthings () {System.out.println ("does other things."); }}