设计模式笔记_结构型_装饰器模式

1.装饰器模式介绍

装饰器模式是一种结构型设计模式,允许你动态地给对象添加行为,而无需修改其代码。它的核心思想是将对象放入一个"包装器"中,这个包装器提供了额外的功能,同时保持原有对象的接口不变。

想象一下,你有一个简单的咖啡,你想让它变得更特别。你可以给它加奶、加糖、加香草等等,但咖啡本身还是咖啡。这些额外的东西不会改变咖啡的本质,只是让它更丰富。

装饰器模式有四个角色:

  1. 抽象组件(Component):可以是一个接口或者抽象类,规定了被装饰对象的行为;
  2. 具体组件(ConcreteComponent):实现或继承Component的一个具体对象,也即被装饰对象;
  3. 抽象装饰器(Decorator):一般是抽象类, 继承或实现抽象组件Component;其内部必然有一个属性指向Component组件对象;通过其子类 ConcreteDecorator 扩展具体构件的功能。
  4. 具体装饰器(ConcreteDecorator):Decorator的具体实现类,理论上每个ConcreteDecorator 都扩展了 Component 对象的一种功能;

四个角色的关系:

2.代码演示

**抽象组件(Component):**咖啡接口定义了咖啡有"描述"和"价格"两个方法

java 复制代码
// 基础接口
interface Coffee {
    String getDescription();
    double getCost();
}

**具体组件(ConcreteComponent):**咖啡实现类,是需要被装饰的对象

java 复制代码
// 具体的咖啡类
class SimpleCoffee implements Coffee {
    @Override
    public String getDescription() {
        return "Simple Coffee";
    }

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

**抽象装饰器(Decorator):**抽象类,实现咖啡接口,内部有一个属性指向Coffee对象

java 复制代码
// 装饰器基类
abstract class CoffeeDecorator implements Coffee {
    //内部必然有一个属性指向Component组件对象
    protected Coffee coffee;

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

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

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

**具体装饰器(ConcreteDecorator):**每个Coffee装饰器都扩展了Coffee对象的一种功能

java 复制代码
// 具体的装饰器类
class MilkDecorator extends CoffeeDecorator {
    public MilkDecorator(Coffee coffee) {
        super(coffee);
    }

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

    @Override
    public double getCost() {
        return coffee.getCost() + 1.5;
    }
}

class SugarDecorator extends CoffeeDecorator {
    public SugarDecorator(Coffee coffee) {
        super(coffee);
    }

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

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

使用装饰器: 每个装饰器都可以单独或组合使用,给咖啡增加不同的特性。通过这种方式,原来的咖啡类保持不变,而我们可以灵活地为它添加新功能

java 复制代码
// 使用装饰器模式
public class CoffeeShop {
    public static void main(String[] args) {
        //创建简单的咖啡类SimpleCoffee
        Coffee coffee = new SimpleCoffee();
        System.out.println(coffee.getDescription() + " $" + coffee.getCost());

        //通过装饰器MilkDecorator给咖啡加奶
        coffee = new MilkDecorator(coffee);
        System.out.println(coffee.getDescription() + " $" + coffee.getCost());

        //通过装饰器SugarDecorator给咖啡加糖
        coffee = new SugarDecorator(coffee);
        System.out.println(coffee.getDescription() + " $" + coffee.getCost());
    }
}
相关推荐
ZC跨境爬虫1 天前
跟着 MDN 学 HTML day_9:(信件语义标记)
前端·css·笔记·ui·html
灰子学技术1 天前
Envoy 使用的设计模式技术文档
设计模式
OBiO20131 天前
Cell | 突破AAV载体容量限制!路中华/姜玉武/刘太安团队开发AAVLINK系统实现大基因递送
笔记
智者知已应修善业1 天前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
sakiko_1 天前
UIKit学习笔记5-使用UITableView制作聊天页面
笔记·学习·swift·uikit
Alice-YUE1 天前
【js高频八股】防抖与节流
开发语言·前端·javascript·笔记·学习·ecmascript
小陈phd1 天前
TensorRT 入门完全指南(一)——从核心定义到生态工具全解析
人工智能·笔记
是上好佳佳佳呀1 天前
【前端(十一)】JavaScript 语法基础笔记(多语言对比)
前端·javascript·笔记
handler011 天前
Linux 内核剖析:进程优先级、上下文切换与 O(1) 调度算法
linux·运维·c语言·开发语言·c++·笔记·算法
其实防守也摸鱼1 天前
CTF密码学综合教学指南--第四章
网络·笔记·安全·网络安全·密码学·ctf