You get to know three minutes with a decorative pattern

Text: 959 words, estimated reading time: 8 minutes

definition:

  Decorative patterns (Decorator) to dynamically add some additional responsibilities to an object. Increased functionality, the decorative pattern is more flexible than a subclass.
  Here you can give an example of a life, a cake, put fruit on the cake, the cake becomes a fruit cake, fruit cake a candle to this, it becomes a birthday cake. (This is an example of Head First, personally I feel very image, remember).

analysis:

  If we need to extend the functionality of a class, how would you do it? If you directly modify the class, we would violate the principle of opening and closing (closed for modification, open to extension).
  We can extend this class, write a sub-class of this class, for extended functionality, you can also use a combination of (this class as a member variable), to achieve the same effect.
  We will inherit this relationship is called is-a, this combination of relationship is called use-a. The degree of coupling is-a higher than use-a, so we can often hear this argument: a combination of superior inheritance, reason is that different levels of coupling of these two relationships.
The core idea of ​​decorative patterns, in fact, instead of using a combination of inheritance.

Graphic:

Example:

  To give an example of a coffee shop, coffee raw coffee beans, we can use coffee beans and milk, honey, mocha combination of different prices, different flavors of coffee.
  Here coffee beans is to be decorated objects, which is illustrated in ConcreteComponent, beverages that we abstracted Component, defines two methods show price and material. Milk, honey, mocha decorative objects, which is the figure ConcreteDecoratorA, ConcreteDecoratorB. They abstracted the Decorator, also defines two methods show price and material, in particular the class diagram and achieve the following:

 

 

Code:

/**
 * 饮品.
 *
 * @author jialin.li
 * @date 2019-12-26 22:58
 */
public interface Beverage {
    /** 获取描述 */
    String getDescription();
    /** 获取金额 */
    double getPrice();
}
/**
 * 咖啡豆1
 *
 * @author jialin.li
 * @date 2019-12-26 22:59
 */
public class CoffeeBean1 implements Beverage {

    @Override
    public String getDescription() {
        return "第一种咖啡豆";
    }

    @Override
    public double getPrice() {
        return 10d;
    }
}
/**
 * 咖啡豆2
 *
 * @author jialin.li
 * @date 2019-12-26 23:00
 */
public class CoffeeBean2 implements Beverage{
    @Override
    public String getDescription() {
        return "第一种咖啡豆";
    }

    @Override
    public double getPrice() {
        return 12.5d;
    }
}
/**
 * 装饰器.
 *
 * @author jialin.li
 * @date 2019-12-26 23:02
 */
public class Decorator implements Beverage{

    protected Beverage coffee;

    @Override
    public String getDescription() {
        return "装饰器,由子类重写方法";
    }

    @Override
    public double getPrice() {
        return 0;
    }
}
/**
 * 蜂蜜.
 *
 * @author jialin.li
 * @date 2019-12-26 23:07
 */
public class Honey extends Decorator {
    public Honey(Beverage coffee) {
        this.coffee = coffee;
    }

    @Override
    public String getDescription() {
        return coffee.getDescription() + "加蜂蜜";
    }

    @Override
    public double getPrice() {
        return coffee.getPrice() + 4.5d;
    }
}
/**
 * 牛奶.
 *
 * @author jialin.li
 * @date 2019-12-26 23:03
 */
public class Milk extends Decorator {
    public Milk(Beverage coffee) {
        this.coffee = coffee;
    }

    @Override
    public String getDescription() {
        return coffee.getDescription() + "加牛奶";
    }

    @Override
    public double getPrice() {
        return coffee.getPrice() + 1.5d;
    }
}
/**
 * 摩卡.
 *
 * @author jialin.li
 * @date 2019-12-26 23:05
 */
public class Mocha extends Decorator {
    public Mocha(Beverage coffee) {
        this.coffee = coffee;
    }

    @Override
    public String getDescription() {
        return coffee.getDescription() + "加摩卡";
    }

    @Override
    public double getPrice() {
        return coffee.getPrice() + 2.5d;
    }
}
/**
 * 测试类.
 *
 * @author jialin.li
 * @date 2019-12-26 23:09
 */
public class Main {
    public static void main(String[] args) {
        CoffeeBean1 coffee1 = new CoffeeBean1();
        CoffeeBean2 coffee2 = new CoffeeBean2();

        // 加蜂蜜
        Beverage honey = new Honey(coffee1);
        // 加摩卡
        Beverage mocha = new Mocha(honey);
        System.out.println(mocha.getDescription());
        System.out.println(mocha.getPrice());

        // 加牛奶
        Milk milk = new Milk(coffee2);
        System.out.println(milk.getDescription());
        System.out.println(milk.getPrice());
    }
}

结果:

  第一种咖啡豆加蜂蜜加摩卡
  17.0
  第一种咖啡豆加牛奶
  14.0

Guess you like

Origin www.cnblogs.com/nedulee/p/12105111.html