package com.lbx.concreteDecorator;
import com.lbx.component.Component;
import com.lbx.decorator.Decorator;
/**
* 具体装饰(这里演示了两种扩展的情况,听音乐+吃饭和吃饭+唱歌)
* @author Administrator
*
*/
public class ConcreteDecoratorListen extends Decorator {
public ConcreteDecoratorListen(Component component) {
super(component);
}
public void eat(){
this.listen("听音乐"); //执行增加的功能
super.eat();
}
private void listen(Object obj){
System.out.println(obj);
}
}
具体装饰2
package com.lbx.concreteDecorator;
import com.lbx.component.Component;
import com.lbx.decorator.Decorator;
public class ConcreteDecoratorSing extends Decorator {
public ConcreteDecoratorSing(Component component) {
super(component);
// TODO Auto-generated constructor stub
}
public void eat() {
super.eat();
System.out.println(sing()); // 执行扩展功能
}
private String sing() {
return "唱歌";
}
}
测试方法
package com.lbx.test;
import com.lbx.component.Component;
import com.lbx.conceteComponent.ConcreteComponent;
import com.lbx.concreteDecorator.ConcreteDecoratorListen;
import com.lbx.concreteDecorator.ConcreteDecoratorSing;
public class Test {
public static void main(String[] args) {
Component component = new ConcreteComponent();
ConcreteDecoratorListen c = new ConcreteDecoratorListen(component);
c.eat();
ConcreteDecoratorSing c2 = new ConcreteDecoratorSing(component);
c2.eat();
}
}