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());
    }
}

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

相关推荐
Ylucius33 分钟前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
七夜zippoe1 小时前
分布式系统实战经验
java·分布式
是梦终空1 小时前
JAVA毕业设计176—基于Java+Springboot+vue3的交通旅游订票管理系统(源代码+数据库)
java·spring boot·vue·毕业设计·课程设计·源代码·交通订票
落落落sss1 小时前
sharding-jdbc分库分表
android·java·开发语言·数据库·servlet·oracle
码爸1 小时前
flink doris批量sink
java·前端·flink
Monodye2 小时前
【Java】网络编程:TCP_IP协议详解(IP协议数据报文及如何解决IPv4不够的状况)
java·网络·数据结构·算法·系统架构
一丝晨光2 小时前
逻辑运算符
java·c++·python·kotlin·c#·c·逻辑运算符
无名指的等待7123 小时前
SpringBoot中使用ElasticSearch
java·spring boot·后端
Tatakai253 小时前
Mybatis Plus分页查询返回total为0问题
java·spring·bug·mybatis
武子康3 小时前
大数据-133 - ClickHouse 基础概述 全面了解
java·大数据·分布式·clickhouse·flink·spark