知识卡片
装饰模式的定义与四个角色
装饰以共同接口包装对象,在委托基础上动态叠加职责。Component规定能力,具体构件提供原实现,Decorator持有Component并转发,具体装饰者添加前后行为。
包装对象可以再被包装,因此不同增强可组合,而不必为每种组合写子类。仍须保持使用契约,不能保证身份或全部可观察语义不变。图按代码operate签名;2包1时实际为method1→原操作→method2,未运行。
结构图:
classDiagram
class Component {
<<abstract>>
+operate() void
}
class ConcreteComponent {
+operate() void
}
class Decorator {
<<abstract>>
+Decorator(Component component)
+operate() void
}
class ConcreteDecorator1 {
+ConcreteDecorator1(Component component)
-method1() void
+operate() void
}
class ConcreteDecorator2 {
+ConcreteDecorator2(Component component)
-method2() void
+operate() void
}
Component <|-- ConcreteComponent
Component <|-- Decorator
Decorator --> Component : 包装并委托
Decorator <|-- ConcreteDecorator1
Decorator <|-- ConcreteDecorator2
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 17.2 装饰模式的定义 · 查看原书依据
Attach additional responsibilities to an object dynamically keeping the same interface.Decorators provide a flexible alternative t
参考来源
- 位置:《设计模式之禅(第2版)》17.2 装饰模式的定义(来源项目book-sources,源文件:extracted/设计模式之禅(第2版)/_epub-src/text/part0077.html)
- 结论依据:原文定义、示例及取舍支撑本卡;已知过强表述在正文限定。
- 原始内容:Attach additional responsibilities to an object dynamically keeping the same interface.Decorators provide a flexible alternative t