原版设计模式之组合模式(Composite)

Intent (定义)

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.(将对象组合成树形结构,以表示整体和部分的层次。组合模式能让客户端无差别的对待独立的个体和组合的对象

Motivation (详情)

在这里插入图片描述
如在绘图编辑器中,让用户用简单的组件构建复杂的图表。用户可以将组件分组以形成更大的组件,而这些组件又可以分组以形成更大的组件。
如上图所示,有一个容器类Graphic和其他子类,Line,Rectangle,Text作为叶子节点,没有组合其他子类,可以直接绘制图形。
Picture组合其他子类,绘制更复杂的图形。

The key to the Composite pattern is an abstract class that represents both primitives and their containers.(组合模式的关键在于定义一个抽象类,这个抽象类可以表示原始对象,也可以作为容器。) For the graphics system, this class is Graphic.Graphic declares operations like Draw that are specific to graphical objects(在图形绘制系统中,这个类就是Graphic。Graphic声明了Draw方法用来绘制特定的图形,
It also declares operations that all composite objects share, such as operations for accessing and managing its children.(还声明了所有组合对象共享的操作,例如访问和管理其子类
The subclasses Line, Rectangle, and Text (see preceding class diagram) define primitive graphical objects(其子类Line,Rectangle,Text定义了原始图形对象). These classes implement Draw to draw lines, rectangles, and text, respectively(这些类实现Draw方法,分别用来绘制线条,多边形和文字). Since primitive graphics have no child graphics, none of these subclasses implements child-related operations(这些原始图形没有子图形,也没有实现子类相关的操作。).
The Picture class defines an aggregate of Graphic objects. Picture implements Draw to call Draw on its children, and it implements child-related operations accordingly. (Picture类定义了Graphic对象的聚合,Picture实现了Draw方法去调用其子类的Draw方法,同时也实现了相应子类相关操作。
Because the Picture interface conforms to the Graphic interface, Picture objects can compose other Pictures recursively.(因为Picture接口继承Graphic接口,因此Picture对象能递归的组合其他图形。
如此形成了如下这种组合样式:
在这里插入图片描述
参考代码

Applicability(适用点)

  • you want to represent part-whole hierarchies of objects. (用来表示整体和部分对象
  • you want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.(希望客户端忽视组合对象和独立对象的不同点,以同样的方式来对待组合结构中的所有对象。

Structure (结构)

在这里插入图片描述
在这里插入图片描述

Participants (类说明)

  • Component (Graphic) (组件
    • declares the interface for objects in the composition. (声明组合对象中的接口
    • implements default behavior for the interface common to all classes,as appropriate. (看实际情况,实现所有类通用的默认接口行为
    • declares an interface for accessing and managing its child components.(声明访问和管理子组件的接口
    • (optional) defines an interface for accessing a component’s parent in the recursive structure, and implements it if that’s appropriate.(【可选】定义在递归结构中访问组件父组件的方法,并根据实际情况实现这个方法
  • Leaf (Rectangle, Line, Text, etc.) (叶子节点
    • represents leaf objects in the composition. A leaf has no children. (表示组合中叶子对象,叶子节点没有子节点
    • defines behavior for primitive objects in the composition.(定义组合中原始对象的行为
  • Composite (Picture) (复合对象
    • defines behavior for components having children.(定义拥有子节点的组件行为
    • stores child components. (存储子节点
    • implements child-related operations in the Component interface.(实现Component中与子元素相关操作的接口
  • Client (客户端
    • manipulates objects in the composition through the Component interface(通过Component接口操作组合中的对象

Collaborations (约定)

Clients use the Component class interface to interact with objects in the composite structure(客户端使用Component中的方法和组合结构中的对象交互). If the recipient is a Leaf, then the request is handled directly(如果接收对象是叶子节点,那么就直接处理请求). If the recipient is a Composite, then it usually forwards requests to its child components, possibly performing additional operations before and/or after forwarding.(如果接收对象是复合对象,它就会将请求转发给子组件,也可能会在转发前后进行其他操作。

Related Patterns (相关模式)

  • Often the component-parent link is used for a Chain of Responsibility(通常,组件,父组件关联被用在责任链中
  • Decorator (196) is often used with Composite. When decorators and composites are used together, they will usually have a common parent class. So decorators will have to support the Component interface with operations like Add, Remove, and GetChild.(装饰器经常和组合一起使用。当装饰者和组合一起用时,通常会有一个父类。因此装饰者需要支持Component中像Add,Remove,Getchild这样的操作。
  • Flyweight (218) lets you share components, but they can no longer refer to their parents.(享元模式能共享组件,但是不再指向他们的父组件。
  • Iterator (289) can be used to traverse composites.(迭代器模式被用来遍历复合对象
  • Visitor (366) localizes operations and behavior that would otherwise be distributed across Composite and Leaf classes.(访问者模式使操作和行为局部化,否则就要分布在复合类和叶子类中【尚未理解何意,仅翻译出来】
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值