设计模式-装饰设计模式

public class WrapperTest {
public static void main(String[] args) {
/**
拍照
*/
// Componnet conCreateComponnet = new ConCreateComponnet();
// conCreateComponnet.operation();

/**
添加美颜
拍照
*/
// Componnet decorator = new ConDecorator1(new ConCreateComponnet());
// decorator.operation();

/**
添加滤镜
添加美颜
拍照
*/
Componnet decorator = new ConDecorator2(new ConDecorator1(new ConCreateComponnet()));
decorator.operation();
}
}

interface Componnet {
void operation();
}

class ConCreateComponnet implements Componnet {
@Override
public void operation() {
System.out.println("拍照");
}
}


abstract class Decorator implements Componnet {
protected Componnet componnet;
public Decorator(Componnet componnet) {
this.componnet = componnet;
}
}

class ConDecorator1 extends Decorator {
public ConDecorator1(Componnet componnet) {
super(componnet);
}

@Override
public void operation() {
System.out.println("添加美颜");
componnet.operation();
}
}

class ConDecorator2 extends ConDecorator1 {
public ConDecorator2(Componnet componnet) {
super(componnet);
}

@Override
public void operation() {
System.out.println("添加滤镜");
componnet.operation();
}
}
posted @ 2022-02-25 15:00  剑阁丶神灯  阅读(17)  评论(0编辑  收藏  举报