package com.dylan.gof.pattern.decorator;
/**
* Generated by StarUML(tm) Java Add-In
* @ Project : Untitled
* @ File Name : Decorator.java
* @ Date : 2011-2-16
* @ Author : Dylan
*
* @desc:
* 抽象构件:
* 原始的功能接口 ,为组件和装饰的公共父类,它定义了子类必须实现的方法。
*/
public interface Component {
public void go();
}
具体构建角色(具体的原始功能类.是一个具体的组件类,可以通过给它添加装饰来增加新的功能
package com.dylan.gof.pattern.decorator;
/**
* Generated by StarUML(tm) Java Add-In
* @ Project : Untitled
* @ File Name : Decorator.java
* @ Date : 2011-2-16
* @ Author : Dylan
*
* @desc:
* 具体构件:
* 具体的原始功能类.是一个具体的组件类,可以通过给它添加装饰来增加新的功能
*/
public class ConcreteComponent implements Component {
public void go() {
System.out.println("行走");
}
}
装饰角色:持有具体构件类的对象,以便执行原有功能,是所有装饰的公共父类,它定义了所有装饰必须实现的方法,
* 同时,它还保存了一个对于Component的引用,以便将用户的请求转发给Component,并可能在转发请求前后执行一些
* 附加的动作。
package com.dylan.gof.pattern.decorator;
/**
* Generated by StarUML(tm) Java Add-In
* @ Project : Untitled
* @ File Name : Decorator.java
* @ Date : 2011-2-16
* @ Author : Dylan
*
* @describle
* 装饰角色:持有具体构件类的对象,以便执行原有功能,是所有装饰的公共父类,它定义了所有装饰必须实现的方法,
* 同时,它还保存了一个对于Component的引用,以便将用户的请求转发给Component,并可能在转发请求前后执行一些
* 附加的动作。
*/
public class Decorator implements Component {
public Component component;
protected Decorator(Component component) {
this.component = component;
}
public void go() {
this.component.go();
}
}
具体装饰(这里演示了两种扩展的情况,走路+听音乐和唱歌s)
package com.dylan.gof.pattern.decorator;
/**
* Generated by StarUML(tm) Java Add-In
* @ Project : Untitled
* @ File Name : Decorator.java
* @ Date : 2011-2-16
* @ Author : Dylan
*
* @desc
* 具体装饰(这里演示了两种扩展的情况,走路+听音乐和唱歌s)
*/
//
public class ConcreteDecoratorSing extends Decorator {
protected ConcreteDecoratorSing(Component component) {
super(component);
// code is here
}
public void go() {
listen("听音乐");// 执行扩展功能
super.go();
}
private void listen(Object obj) {
System.out.println(obj);
}
}
package com.dylan.gof.pattern.decorator;
/**
* Generated by StarUML(tm) Java Add-In
* @ Project : Untitled
* @ File Name : Decorator.java
* @ Date : 2011-2-16
* @ Author : Dylan
*/
//具体装饰(这里演示了两种扩展的情况,走路+听音乐和唱歌s)
//具体的装饰,可以使用它们来装饰具体的Component
public class ConcreteDecoratorListenDump extends Decorator {
protected ConcreteDecoratorListenDump(Component component) {
super(component);
// code is here
}
public void go() {
component.go();
System.out.println(dump());
// 执行扩展功能
}
private String dump() {
return "跳舞";
}
}
package com.dylan.gof.pattern.decorator;
/**
* Generated by StarUML(tm) Java Add-In
* @ Project : Untitled
* @ File Name : Decorator.java
* @ Date : 2011-2-16
* @ Author : Dylan
*/
//具体装饰(这里演示了两种扩展的情况,走路+听音乐和唱歌s)
//具体的装饰,可以使用它们来装饰具体的Component
public class ConcreteDecoratorListen extends Decorator {
protected ConcreteDecoratorListen(Component component) {
super(component);
// code is here
}
public void go() {
component.go();
System.out.println(sing());
// 执行扩展功能
}
private String sing() {
return "唱歌";
}
}
package com.dylan.gof.pattern.decorator;
/**
* Generated by StarUML(tm) Java Add-In
* @ Project : Untitled
* @ File Name : Decorator.java
* @ Date : 2011-2-16
* @ Author : Dylan
*/
public class Client {
public static void main(String[] args) {
Component component = new ConcreteComponent();
ConcreteDecoratorListen cdl = new ConcreteDecoratorListen(component);
cdl.go();
System.out.println();
Component cds = new ConcreteDecoratorSing(component);
cds.go();
System.out.println();
Component dump=new ConcreteDecoratorListenDump(cdl);
dump.go();
}
}