• 装饰者模式


    咖啡问题

    现需要完成一个咖啡下单的问题:

    1. 咖啡种类:Espresso、ShortBlack、LongBlack、Decaf
    2. 调料:Milk、Soy、Chocolate
    3. 要求在扩展新的咖啡种类时,具有良好的扩展性,改动方便
    4. 使用OO来计算不同种类咖啡的费用,客户可以点单品咖啡,也可以单品咖啡+调料组合

    装饰者模式

    基本介绍

    1. 装饰者模式能够动态的将新的功能附加到对象上,在对象功能扩展方面,它比继承更有弹性,装饰者模式也体现了开闭原则
    2. 装饰者模式装饰者模式就像打包一个快递,其中包含主体(即打包的物品);以及包装(快递包装外壳);一个主体可以由多个包装嵌套装饰

    装饰者模式解决咖啡问题

    在这里插入图片描述

    /***
     * @author shaofan
     * @Description 装饰者模式解决咖啡问题
     */
    public class CoffeeBar {
        public static void main(String[] args) {
            // 一份牛奶,两份巧克力的Espresso
            // 不带任何配料的咖啡,装饰者中的主体
            Drink order = new Espresso();
    
            // 加入一份牛奶,使用牛奶装饰主体
            order = new Milk(order);
    
            // 加入一份巧克力,使用巧克力再次装饰
            for(int i=0;i<2;i++){
                order = new Chocolate(order);
            }
    
            System.out.println(order.cost());
            System.out.println(order.getDescription());
    
        }
    
    }
    abstract class Drink{
        private String desc;
        private double price;
    
        public String getDesc() {
            return desc;
        }
    
        public void setDesc(String desc) {
            this.desc = desc;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
        abstract double cost();
        abstract String getDescription();
    }
    class Decorator extends Drink{
        private Drink drink;
    
        public Decorator(Drink drink){
            this.drink = drink;
        }
    
        @Override
        double cost() {
            return super.getPrice()+drink.cost();
        }
    
        @Override
        String getDescription() {
            return drink.getDescription()+"+"+super.getDesc();
        }
    
    }
    class Coffee extends Drink{
    
        @Override
        double cost() {
            return super.getPrice();
        }
    
        @Override
        String getDescription() {
            return getDesc();
        }
    }
    class Espresso extends Coffee{
        private final String DESC = "Espresso";
        private final double PRICE = 5.0;
        public Espresso(){
            super.setDesc(DESC);
            super.setPrice(PRICE);
        }
    }
    class ShortBlack extends Coffee{
        private final String DESC = "ShortBlack";
        private final double PRICE = 5.0;
        public ShortBlack(){
            super.setDesc(DESC);
            super.setPrice(PRICE);
        }
    }
    class Chocolate extends Decorator{
        private final String DESC = "Chocolate";
        private final double PRICE = 1.0;
        public Chocolate(Drink drink){
            super(drink);
            super.setDesc(DESC);
            super.setPrice(PRICE);
        }
    }
    
    class Milk extends Decorator{
        private final String DESC = "Milk";
        private final double PRICE = 2.0;
        public Milk(Drink drink){
            super(drink);
            super.setDesc(DESC);
            super.setPrice(PRICE);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    源码分析

    在Java的IO中,FilterInputStream就是一个装饰者
    在这里插入图片描述

    在这里插入图片描述
    它的内部组合了一个InputStream,对FileInputStream、StrintgBufferInputStream、ByteArrayInputStream这些主体进行装饰,提供一些额外的功能

  • 相关阅读:
    Node学习-第六章-express中间件与RESful API接口规范(下)
    用友BIP 安装配置专业脚手架开发工具(图文)
    详谈判断点在多边形内的七种方法
    Arduino IDE + Esp32 Cam + 实现视频流 + 开发环境部署
    k8s自动化运维八-如何清理docker存储的大文件
    【头歌实验】一、Python初体验——Hello World
    星火绘镜Typemovie:释放创意,轻松制作你的短视频故事
    人工智能在生物学和神经科学中的应用
    UNIAPP day_00(8.29) 准备运行环境
    第三十六章 Objects - 有用的 ObjectScript 函数
  • 原文地址:https://blog.csdn.net/m0_48468380/article/details/126214734