Java中的装饰器模式

在Java中,装饰器模式允许我们动态地给对象添加新的行为或责任,而无需修改原有类。以下是一个简单的装饰器模式示例,我们将模拟一个咖啡销售系统,其中基础饮料类(Component)是Coffee,装饰器类(Decorator)用来添加额外的调料。

首先,定义抽象组件------基础咖啡接口:

java 复制代码
// 抽象组件(Component)
public interface Coffee {
    double getCost(); // 获取咖啡价格
    String getDescription(); // 获取咖啡描述
}

// 具体组件(ConcreteComponent)
public class SimpleCoffee implements Coffee {
    @Override
    public double getCost() {
        return 1.0; // 基础咖啡的价格
    }

    @Override
    public String getDescription() {
        return "Simple Coffee"; // 基础咖啡的描述
    }
}

接下来,创建装饰器接口:

java 复制代码
// 装饰器接口(Decorator)
public abstract class CoffeeDecorator implements Coffee {
    protected Coffee coffee; // 维持对组件对象的引用

    public CoffeeDecorator(Coffee coffee) {
        this.coffee = coffee;
    }

    @Override
    public double getCost() {
        return coffee.getCost(); // 默认调用原始咖啡的价格
    }

    @Override
    public String getDescription() {
        return coffee.getDescription(); // 默认调用原始咖啡的描述
    }
}

然后,我们创建几个具体的装饰器类,它们会附加额外的功能(比如添加糖、奶或者巧克力):

java 复制代码
// 具体装饰器 - 添加糖
public class SugarDecorator extends CoffeeDecorator {
    private static final double SUGAR_COST = 0.25;

    public SugarDecorator(Coffee coffee) {
        super(coffee);
    }

    @Override
    public double getCost() {
        return super.getCost() + SUGAR_COST; // 在原始咖啡价格基础上增加糖的成本
    }

    @Override
    public String getDescription() {
        return coffee.getDescription() + ", With Sugar"; // 更新描述信息
    }
}

// 具体装饰器 - 添加牛奶
public class MilkDecorator extends CoffeeDecorator {
    private static final double MILK_COST = 0.5;

    public MilkDecorator(Coffee coffee) {
        super(coffee);
    }

    @Override
    public double getCost() {
        return super.getCost() + MILK_COST; // 在原始咖啡价格基础上增加牛奶的成本
    }

    @Override
    public String getDescription() {
        return coffee.getDescription() + ", With Milk"; // 更新描述信息
    }
}

// 可以继续定义其他装饰器,如ChocolateDecorator等...

最后,在客户端代码中,我们可以动态地构建不同口味的咖啡:

java 复制代码
public class CoffeeShop {
    public static void main(String[] args) {
        Coffee simpleCoffee = new SimpleCoffee();
        System.out.println(simpleCoffee.getDescription() + " costs: " + simpleCoffee.getCost());

        Coffee coffeeWithSugar = new SugarDecorator(simpleCoffee);
        System.out.println(coffeeWithSugar.getDescription() + " costs: " + coffeeWithSugar.getCost());

        Coffee coffeeWithMilkAndSugar = new MilkDecorator(coffeeWithSugar);
        System.out.println(coffeeWithMilkAndSugar.getDescription() + " costs: " + coffeeWithMilkAndSugar.getCost());
    }
}

运行上述代码后,将会输出每种咖啡的描述和对应的价格,展示了如何通过装饰器模式动态地添加不同的调料并计算总成本。

相关推荐
Flynn Liang26 分钟前
AiServices 源码级拆解:一个接口如何变成 RAG Agent
java·langchain·源码分析·rag·langchain4j·aiservices
Flynn Liang39 分钟前
LangChain4j 1.23 架构全景:Java 大模型应用的 6 层抽象
java·langchain·大模型·rag·ai agent·langchain4j
聪明的笨猪猪1 小时前
实际工程问题:AI生成Redis分布式锁的原子性缺陷导致库存超卖
java·经验分享·面试·ai编程
软件技术NINI2 小时前
基于SpringBoot和Vue的新能源汽车销售推荐系统
java·vue.js·spring boot·后端·汽车·毕业设计·毕设
Flynn Liang2 小时前
LangChain4j 生产级工程骨架:12 个模块 + Docker 一键启动
java·spring boot·langchain·rag·langchain4j
噢,我明白了4 小时前
JAVA中的一些基础应用知识点
java·开发语言
SL_staff4 小时前
私有化低代码不是妥协,而是企业级数字化的自主可控主航道
java
装不满的克莱因瓶4 小时前
【SpringAI Alibaba】ReactAgent结构化输出OutputType与OutputSchema实战
java·人工智能·ai·springai·springaialibaba
秋天的怀念_David5 小时前
算法一千零一夜 · 第二十八夜 注册
java·程序人生·算法
Java码农也是农5 小时前
Spring 事务详解:@Transactional 如何生效、何时回滚、传播行为怎么选
java·spring boot·spring·事务