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

学习目标:

  • 掌握装饰器模式

学习内容:

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

案例:咖啡订单系统

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

基本组件接口和具体实现

// 基本的咖啡接口
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 对象:

// 装饰器基类
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();
    }
}

具体装饰器实现

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

// 添加牛奶的装饰器
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;
    }
}
// 添加糖的装饰器
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;
    }
}

客户端代码

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

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

运行结果

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 对象。

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


相关推荐

  1. 掌握装饰模式具体例子

    2024-06-05 21:46:03       29 阅读
  2. python中的装饰例子说明

    2024-06-05 21:46:03       26 阅读
  3. 装饰模式

    2024-06-05 21:46:03       59 阅读
  4. 装饰设计模式

    2024-06-05 21:46:03       51 阅读
  5. 装饰模式(Decorator)

    2024-06-05 21:46:03       53 阅读
  6. 装饰模式

    2024-06-05 21:46:03       50 阅读
  7. 装饰模式

    2024-06-05 21:46:03       50 阅读
  8. 装饰模式

    2024-06-05 21:46:03       44 阅读
  9. 装饰模式

    2024-06-05 21:46:03       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-05 21:46:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-05 21:46:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-05 21:46:03       87 阅读
  4. Python语言-面向对象

    2024-06-05 21:46:03       96 阅读

热门阅读

  1. lua拼接字符串

    2024-06-05 21:46:03       28 阅读
  2. 如何有效屏蔽手机上的骚扰电话20240530

    2024-06-05 21:46:03       46 阅读
  3. [论文阅读] ZeRo

    2024-06-05 21:46:03       31 阅读
  4. 关于lua源代码中的EXTRA_STACK宏

    2024-06-05 21:46:03       30 阅读
  5. Pytest的断言与条件判断的区别

    2024-06-05 21:46:03       26 阅读
  6. 基于Hadoop平台的大数据可视化分析实现与应用

    2024-06-05 21:46:03       31 阅读
  7. 深度学习手撕代码题

    2024-06-05 21:46:03       31 阅读
  8. Spark基础:Scala内建控制结构

    2024-06-05 21:46:03       34 阅读
  9. 深度学习常用命令

    2024-06-05 21:46:03       27 阅读