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

学习目标:

  • 掌握装饰器模式

学习内容:

装饰器模式(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 对象。

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


相关推荐
笑笑先生3 分钟前
从接口搬运工到研发控制平面,BFF 到底在解决什么?
前端·架构·node.js
霪霖笙箫4 分钟前
「JS全栈AI Agent学习」二、反思、工具使用、规划——让 Agent 从"执行者"变成"自主完成者"
前端·agent·ai编程
前端缘梦5 分钟前
Next.js全栈项目部署全流程|从0到1解决数据库、WebSocket、图片上传所有坑
前端·全栈·next.js
www_stdio5 分钟前
🚀 从 Event Loop 到 AI Agent:我的 Node.js 全栈进阶之路
前端·node.js·nestjs
www_stdio6 分钟前
拒绝做Git“蜘蛛网”制造者!从分支管理到Rebase,带你走一遍标准开发流
前端·github
Moment7 分钟前
面试爱问底层时,我是怎么读大型前端源码的❓❓❓
前端·javascript·面试
long_songs15 分钟前
纯前端 PNG/JPG 转 PDF 工具(无需服务器,源码分享)
服务器·前端·pdf
rongDang25 分钟前
浏览器模拟发送POST请求
前端·javascript
清汤饺子29 分钟前
OpenSpec:让 AI 编程从"开盲盒"到"先签字再干活"
前端·javascript·后端
用户693717500138430 分钟前
太钻 Android 了,在电鸭刷私活把我自己刷清醒了
android·前端·github