装饰器模式

package com.bigdata.decorator;

import java.util.Comparator;

/**
 * Copyright (c) 2019 leyou ALL Rights Reserved
 * Project: learning
 * Package: com.bigdata.decorator
 * Version: 1.0
 *
 * @author qingzhi.wu
 * @date 2020/6/18 13:39
 */
public class DecoratorTest {

  public static void main(String[] args) {
      Component component = new ConcreateComponent2(new ConcreateComponent1(new ConcreateComponent()));
      component.operation();
  }

}

interface Component{
    void operation();
}

class ConcreateComponent implements Component {

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

abstract class Decorator implements Component{
    Component component;

    public Decorator(Component component) {
        this.component = component;
    }
}

class ConcreateComponent1 extends Decorator{

    public ConcreateComponent1(Component component) {
        super(component);
    }

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

class ConcreateComponent2 extends Decorator{

    public ConcreateComponent2(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        component.operation();
        System.out.println("滤镜");
    }
}


你可能感兴趣的:(设计模式)