Component
public interface Component {
public void doSomething();
}
ConcreteComponent
public class ConcreteComponent implements Component {
@Override
public void doSomething() {
System.out.println("Function ");
}
}Decorator
public class Decorator implements Component {
private Component component;
public Decorator(Component component){
this.component = component;
}
@Override
public void doSomething() {
this.component.doSomething();
}
}
ConcreteDecorator1
public class ConcreteDecorator1 extends Decorator {
public ConcreteDecorator1(Component component) {
super(component);
}
@Override
public void doSomething(){
super.doSomething();
doAnotherthing();
}
private void doAnotherthing(){
System.out.println("Function 1");
}
}
ConcreteDecorator2
public class ConcreteDecorator2 extends Decorator {
public ConcreteDecorator2(Component component) {
super(component);
}
@Override
public void doSomething(){
super.doSomething();
doAnotherthing();
}
private void doAnotherthing(){
System.out.println("Function 2");
}
}
Client
public class Client {
public static void main(String[] args) {
Component component = new ConcreteComponent();
Component component2 = new ConcreteDecorator1(component);
Component component3 = new ConcreteDecorator2(component2);
component3.doSomething();
}
}

2906

被折叠的 条评论
为什么被折叠?



