掌握装饰器模式(具体例子)

学习目标:

  • 掌握装饰器模式

学习内容:

装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许在不改变对象接口的前提下,动态地给对象添加职责(功能)。装饰器模式通过创建一个装饰类(Decorator)来包裹真实对象,从而可以在调用真实对象的方法之前或之后进行额外的操作。

案例:咖啡订单系统

假设我们要设计一个咖啡订单系统,不同的咖啡可以添加不同的配料(如牛奶、糖、巧克力等)。我们可以使用装饰器模式来实现这一需求。

基本组件接口和具体实现

bash 复制代码
// 基本的咖啡接口
interface Coffee {
    String getDescription();
    double getCost();
}

// 具体的咖啡实现:简单咖啡
class SimpleCoffee implements Coffee {
    @Override
    public String getDescription() {
        return "Simple Coffee";
    }

    @Override
    public double getCost() {
        return 5.0;
    }
}

装饰器基类

接下来,定义装饰器基类,实现 Coffee 接口,并持有一个 Coffee 对象:

bash 复制代码
// 装饰器基类
abstract class CoffeeDecorator implements Coffee {
    protected Coffee decoratedCoffee;

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

    @Override
    public String getDescription() {
        return decoratedCoffee.getDescription();
    }

    @Override
    public double getCost() {
        return decoratedCoffee.getCost();
    }
}

具体装饰器实现

实现几个具体的装饰器类,例如添加牛奶和糖的装饰器:

bash 复制代码
// 添加牛奶的装饰器
class MilkDecorator extends CoffeeDecorator {
    public MilkDecorator(Coffee coffee) {
        super(coffee);
    }

    @Override
    public String getDescription() {
        return decoratedCoffee.getDescription() + ", Milk";
    }

    @Override
    public double getCost() {
        return decoratedCoffee.getCost() + 1.5;
    }
}
bash 复制代码
// 添加糖的装饰器
class SugarDecorator extends CoffeeDecorator {
    public SugarDecorator(Coffee coffee) {
        super(coffee);
    }

    @Override
    public String getDescription() {
        return decoratedCoffee.getDescription() + ", Sugar";
    }

    @Override
    public double getCost() {
        return decoratedCoffee.getCost() + 0.5;
    }
}

客户端代码

客户端代码可以根据需要动态地为咖啡添加配料:

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

        coffee = new MilkDecorator(coffee);
        System.out.println(coffee.getDescription() + " $" + coffee.getCost());

        coffee = new SugarDecorator(coffee);
        System.out.println(coffee.getDescription() + " $" + coffee.getCost());
    }
}

运行结果

bash 复制代码
Simple Coffee $5.0
Simple Coffee, Milk $6.5
Simple Coffee, Milk, Sugar $7.0

解释

  • 基本的咖啡接口和实现:Coffee 接口定义了基本方法,SimpleCoffee 实现了这个接口,提供基本的咖啡描述和价格。
  • 装饰器基类:CoffeeDecorator 实现了 Coffee 接口,并持有一个 Coffee 对象,通过构造函数注入。它的方法实现简单地委托给被装饰对象。
  • 具体装饰器类:MilkDecorator 和 SugarDecorator 扩展了 CoffeeDecorator,在基本的 Coffee 方法上添加了新的功能(附加描述和价格)。
  • 客户端代码:客户端代码动态地组合装饰器,为咖啡添加不同的配料。每次添加配料时,都会创建一个新的装饰器对象,包裹之前的 Coffee 对象。

通过这种方式,装饰器模式允许我们在运行时动态地扩展对象的功能,而不需要修改现有代码,遵循了开放/封闭原则。


相关推荐
没事多睡觉6665 分钟前
零基础React + TypeScript 教程
前端·react.js·typescript
liliangcsdn5 分钟前
MySQL存储字节类数据的方案示例
java·前端·数据库
_Kayo_7 分钟前
React useState setState之后获取到的数据一直是初始值
前端·javascript·react.js
谷哥的小弟11 分钟前
HTML5新手练习项目—生命体征监测(附源码)
前端·源码·html5·项目
黎明初时11 分钟前
react基础框架搭建3-配置 Redux:react+router+redux+axios+Tailwind+webpack
前端·react.js·webpack
Object~21 分钟前
2.变量声明
开发语言·前端·javascript
IT_陈寒21 分钟前
Vite 3实战:我用这5个优化技巧让HMR构建速度提升了40%
前端·人工智能·后端
阿干tkl21 分钟前
Linux Web终端连接
linux·运维·前端
大爱编程♡24 分钟前
JAVAEE-前端三剑客
java·前端·java-ee
下雨打伞干嘛24 分钟前
前端学习官网文档
前端·学习