简体   繁体   English

复合图案

[英]Composite Pattern

I have a question regarding composite pattern.我有一个关于复合模式的问题。

Does the base class "Component" act like a pointer to point leaf object in "Composite" class?基类“组件”是否像指向“复合”类中的叶对象的指针?

Edit: Let me ask my question in following words.编辑:让我用下面的话问我的问题。 "What is the relation between Composite and Component class?" “Composite 和 Component 类是什么关系?”

Here is the uml class diagram of the pattern.这是模式的 uml 类图。

替代文字

Relation between composite and component:复合和组件的关系:

1) Leaf and Composite usually implement one interface or one abstract class. 1) Leaf 和 Composite 通常实现一个接口或一个抽象类。 In your diagram they extend Component.在您的图中,它们扩展了 Component。 So, the relation on your diagram is inheritance .所以,你的图表上的关系是继承

2) Composite contains instances of Component. 2) Composite 包含 Component 的实例。 Component, as it occasionally can be Composite, can also contain instances of Component. Component,因为它有时可以是 Composite,也可以包含 Component 的实例。 This is called recursive composition .这称为递归组合 In general, the relation is called aggregation .通常,该关系称为聚合

Component成分

  • is the abstraction for all components, including composite ones是所有组件的抽象,包括复合组件
  • declares the interface for objects in the composition声明组合中对象的接口
  • (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate (可选)定义一个用于访问递归结构中组件父级的接口,并在适当时实现它

Leaf叶子

  • represents leaf objects in the composition表示组合中的叶对象
  • implements all Component methods实现所有组件方法

Composite合成的

  • represents a composite Component (component having children)表示一个复合组件(有子组件的组件)
  • implements methods to manipulate children实现操作孩子的方法
  • implements all Component methods, generally by delegating them to its children实现所有的 Component 方法,通常是通过将它们委托给它的孩子

http://en.wikipedia.org/wiki/Composite_pattern http://en.wikipedia.org/wiki/Composite_pattern

All container and containee classes declare an “is a” relationship to the interface( Component ).所有容器和容器类都声明与接口( Component )的“是一个”关系。

All container classes declare a one-to-many “has a” relationship to the interface.所有容器类都声明与接口的一对多“具有”关系。

More here更多在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM