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

学习目标:

  • 掌握装饰器模式

学习内容:

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

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


相关推荐
变与不变80611 小时前
jQuery细节以及使用应用场景
前端·javascript·jquery
SailingCoder13 小时前
【封闭结束1】端到端交付:Agent 一次请求如何走完全链路
前端·安全·ai·面试·agent·harness
风骏时光牛马15 小时前
AI多模块协同自动化工作流
前端
zhangminghuan15 小时前
前端面试应对技巧
前端·面试·职场和发展
计算机魔术师15 小时前
潜伏14年的PHP致命洞,AI跑两天就交卷了
前端
IT_陈寒15 小时前
Vue的这个响应式陷阱我居然现在才发现
前端·人工智能·后端
Python自动化直播16 小时前
智能脚本批量管理拆开看:导入、导出、重写、试听
前端·python·ai·秒播
开开心心就好17 小时前
发票信息录入太麻烦?离线工具一键转表格
运维·服务器·前端·pdf·ocr·maven·intellij-idea
এ慕ོ冬℘゜17 小时前
ES Promise.all/ Promise.race
前端·javascript·elasticsearch