元素码农
基础
UML建模
数据结构
算法
设计模式
网络
TCP/IP协议
HTTPS安全机制
WebSocket实时通信
数据库
sqlite
postgresql
clickhouse
后端
rust
go
java
php
mysql
redis
mongodb
etcd
nats
zincsearch
前端
浏览器
javascript
typescript
vue3
react
游戏
unity
unreal
C++
C#
Lua
App
android
ios
flutter
react-native
安全
Web安全
测试
软件测试
自动化测试 - Playwright
人工智能
Python
langChain
langGraph
运维
linux
docker
工具
git
svn
🌞
🌙
目录
▶
设计原则
单一职责原则
开闭原则
里氏替换原则
依赖倒置原则
接口隔离原则
迪米特法则
▶
创建型模式
工厂方法模式
抽象工厂
单例模式
建造者模式
原型模式
▶
结构型模式
适配器模式
装饰器模式
代理模式
外观模式
组合模式
桥接模式
享元模式
▶
行为型模式
策略模式
观察者模式
命令模式
模板方法模式
状态模式
责任链模式
迭代器模式
中介者模式
访问者模式
备忘录模式
解释器模式
发布时间:
2025-03-21 15:22
↑
☰
# 装饰器模式 ## 概述 装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。 ## 问题场景 在软件开发中,我们经常会遇到以下场景: 1. 需要在运行时动态地给对象添加额外的职责 2. 不能或不想使用继承来扩展功能 3. 需要保持类的职责单一,同时又要实现功能的组合 ## 解决方案 装饰器模式通过将对象包装在装饰器类中,以便动态地修改其行为: ```mermaid classDiagram class Component { <<interface>> +operation() } class ConcreteComponent { +operation() } class Decorator { -component +operation() } class ConcreteDecoratorA { +operation() +addedBehavior() } class ConcreteDecoratorB { +operation() +addedBehavior() } Component <|.. ConcreteComponent Component <|.. Decorator Decorator o-- Component Decorator <|-- ConcreteDecoratorA Decorator <|-- ConcreteDecoratorB ``` 主要角色: 1. 抽象组件(Component): 定义一个对象接口 2. 具体组件(ConcreteComponent): 定义一个具体的对象 3. 装饰器(Decorator): 维持一个指向Component对象的引用 4. 具体装饰器(ConcreteDecorator): 负责给组件对象添加新的职责 ## 代码示例 ### 1. 基本实现 ```go // Component 定义抽象组件接口 type Component interface { Operation() string } // ConcreteComponent 定义具体组件 type ConcreteComponent struct{} func (c *ConcreteComponent) Operation() string { return "ConcreteComponent" } // Decorator 定义抽象装饰器 type Decorator struct { component Component } func (d *Decorator) Operation() string { if d.component != nil { return d.component.Operation() } return "" } // ConcreteDecoratorA 定义具体装饰器A type ConcreteDecoratorA struct { Decorator } func NewConcreteDecoratorA(c Component) Component { return &ConcreteDecoratorA{Decorator{component: c}} } func (d *ConcreteDecoratorA) Operation() string { return "ConcreteDecoratorA(" + d.Decorator.Operation() + ")" } // ConcreteDecoratorB 定义具体装饰器B type ConcreteDecoratorB struct { Decorator } func NewConcreteDecoratorB(c Component) Component { return &ConcreteDecoratorB{Decorator{component: c}} } func (d *ConcreteDecoratorB) Operation() string { return "ConcreteDecoratorB(" + d.Decorator.Operation() + ")" } ``` ### 2. 实际应用示例 ```go // 以咖啡订单系统为例 // Coffee 定义咖啡接口 type Coffee interface { Cost() float64 Description() string } // SimpleCoffee 基础咖啡 type SimpleCoffee struct{} func (c *SimpleCoffee) Cost() float64 { return 10 } func (c *SimpleCoffee) Description() string { return "Simple Coffee" } // MilkDecorator 牛奶装饰器 type MilkDecorator struct { coffee Coffee } func NewMilkDecorator(c Coffee) Coffee { return &MilkDecorator{coffee: c} } func (m *MilkDecorator) Cost() float64 { return m.coffee.Cost() + 2 } func (m *MilkDecorator) Description() string { return m.coffee.Description() + ", Milk" } // SugarDecorator 糖装饰器 type SugarDecorator struct { coffee Coffee } func NewSugarDecorator(c Coffee) Coffee { return &SugarDecorator{coffee: c} } func (s *SugarDecorator) Cost() float64 { return s.coffee.Cost() + 1 } func (s *SugarDecorator) Description() string { return s.coffee.Description() + ", Sugar" } // 使用示例 func main() { coffee := &SimpleCoffee{} coffeeWithMilk := NewMilkDecorator(coffee) coffeeWithMilkAndSugar := NewSugarDecorator(coffeeWithMilk) fmt.Printf("Cost: %.2f, Description: %s\n", coffeeWithMilkAndSugar.Cost(), coffeeWithMilkAndSugar.Description()) // 输出: Cost: 13.00, Description: Simple Coffee, Milk, Sugar } ``` ## 适用场景 1. 动态扩展功能 - 在不修改原有代码的情况下添加功能 - 运行时动态组合功能 2. 避免类爆炸 - 当需要大量组合功能时 - 使用装饰器比创建子类更灵活 3. 单一职责 - 将复杂的功能分解成多个装饰器 - 每个装饰器负责一个特定的功能 ## 优缺点 ### 优点 1. 灵活性高 - 比继承更灵活 - 可以动态组合功能 2. 符合开闭原则 - 不修改现有代码 - 通过新的装饰器扩展功能 3. 遵循单一职责 - 每个装饰器专注于一个功能 - 便于维护和测试 ### 缺点 1. 可能产生大量小对象 - 每个装饰器都是一个对象 - 可能影响系统性能 2. 装饰器顺序敏感 - 多个装饰器的顺序可能影响结果 - 需要注意装饰顺序 ## 实现要点 1. 接口设计 - 保持接口简单清晰 - 确保装饰器和组件接口一致 2. 装饰器实现 - 正确维护对组件的引用 - 实现必要的功能扩展 3. 组合使用 - 合理设计装饰器的组合方式 - 注意装饰顺序的影响 ## 相关模式 1. 适配器模式 - 适配器改变接口 - 装饰器保持接口不变 2. 组合模式 - 组合模式构建树形结构 - 装饰器动态添加功能 3. 策略模式 - 策略模式改变算法 - 装饰器添加功能 ## 总结 装饰器模式是一种灵活的设计模式,它通过对象组合而非继承来扩展对象的功能。这种模式的核心价值在于: 1. 灵活性 - 运行时动态扩展 - 功能自由组合 2. 可维护性 - 符合开闭原则 - 遵循单一职责 3. 可复用性 - 装饰器可以复用 - 组合方式灵活 在使用装饰器模式时,需要注意控制装饰器的数量和复杂度,避免过度使用导致系统难以理解和维护。同时,要注意装饰顺序对结果的影响,确保系统行为符合预期。