返回主题:结构型模式

知识卡片

结构图卡 · 1857.a

组合模式的定义与三个角色

理解等级:未标注

组合把对象组织成部分—整体树,使个体与组合可通过共同构件契约使用。Component定义共有能力,Leaf没有子构件,Composite容纳子Component并提供管理入口。

一致性针对共同能力,不保证所有操作都无需区分叶与分支;本例安全式遍历仍判类型。继承/子对象引用也不自动保证无环、唯一父节点或正确聚合行为。图按代码doSomething/getChildren等签名,未运行。

结构图:

classDiagram
class Component {
 <<abstract>>
 +doSomething() void
}
class Leaf
class Composite {
 +add(Component component) void
 +remove(Component component) void
 +getChildren() ArrayList~Component~
}
Component <|-- Leaf
Component <|-- Composite
Composite o-- Component : 组合子构件

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《设计模式之禅(第2版)》21.2 组合模式的定义(来源项目book-sources,源文件:extracted/设计模式之禅(第2版)/_epub-src/text/part0095.html) - 结论依据:原文定义、示例及取舍支撑本卡;已知过强表述在正文限定。 - 原始内容:Compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compo