组合模式

转载 2015年07月08日 09:55:18

原文地址:http://www.oodesign.com/composite-pattern.html


Composite Pattern


Motivation

There are times when a program needs to manipulate a tree data structure and it is necessary to treat both Branches as well as Leaf Nodes uniformly. Consider for example a program that manipulates a file system. A file system is a tree structure that contains Branches which are Folders as well as Leaf nodes which are Files. Note that a folder object usually contains one or more file or folder objects and thus is a complex object where a file is a simple object. Note also that since files and folders have many operations and attributes in common, such as moving and copying a file or a folder, listing file or folder attributes such as file name and size, it would be easier and more convenient to treat both file and folder objects uniformly by defining a File System Resource Interface.


Intent

  • The intent of this pattern is to compose objects into tree structures to represent part-whole hierarchies.
  • Composite lets clients treat individual objects and compositions of objects uniformly.

Implementation

The figure below shows a UML class diagram for the Composite Pattern:
Composite Pattern Implementation - UML Class Diagram 

  • Component - Component is the abstraction for leafs and composites. It defines the interface that must be implemented by the objects in the composition. For example a file system resource defines move, copy, rename, and getSize methods for files and folders.
  • Leaf - Leafs are objects that have no children. They implement services described by the Component interface. For example a file object implements move, copy, rename, as well as getSize methods which are related to the Component interface.
  • Composite - A Composite stores child components in addition to implementing methods defined by the component interface. Composites implement methods defined in the Component interface by delegating to child components. In addition composites provide additional methods for adding, removing, as well as getting components.
  • Client - The client manipulates objects in the hierarchy using the component interface.

A client has a reference to a tree data structure and needs to perform operations on all nodes independent of the fact that a node might be a branch or a leaf. The client simply obtains reference to the required node using the component interface, and deals with the node using this interface; it doesn�t matter if the node is a composite or a leaf.


Applicability & Examples

The composite pattern applies when there is a part-whole hierarchy of objects and a client needs to deal with objects uniformly regardless of the fact that an object might be a leaf or a branch. 

Example - Graphics Drawing Editor.

In graphics editors a shape can be basic or complex. An example of a simple shape is a line, where a complex shape is a rectangle which is made of four line objects. Since shapes have many operations in common such as rendering the shape to screen, and since shapes follow a part-whole hierarchy, composite pattern can be used to enable the program to deal with all shapes uniformly.

In the example we can see the following actors:
  • Shape (Component) - Shape is the abstraction for Lines, Rectangles (leafs) and and ComplexShapes (composites).
  • Line, Rectangle (Leafs) - objects that have no children. They implement services described by the Shape interface.
  • ComplexShape (Composite) - A Composite stores child Shapes in addition to implementing methods defined by the Shape interface.
  • GraphicsEditor (Client) - The GraphicsEditor manipulates Shapes in the hierarchy.

Alternative Implementation: Note that in the previous example there were times when we have avoided dealing with composite objects through the Shape interface and we have specifically dealt with them as composites (when using the method addToShape()). To avoid such situations and to further increase uniformity one can add methods to add, remove, as well as get child components to the Shape interface. The UML diagram below shows it:

Composite Pattern Alternative Implementation - UML Class Diagram 


Specific problems and implementation

Graphics Editors use composite pattern to implement complex and simple graphics as previously explained.

File System implementations use the composite design pattern as described previously.


Consequences

  • The composite pattern defines class hierarchies consisting of primitive objects and composite objects. Primitive objects can be composed into more complex objects, which in turn can be composed.
  • Clients treat primitive and composite objects uniformly through a component interface which makes client code simple.
  • Adding new components can be easy and client code does not need to be changed since client deals with the new components through the component interface.

Related Patterns

Decorator Pattern - Decorator 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. 

Known Uses

File System Implementation as discussed previously.

Graphics Editors as discussed previously.


结构型模式之——组合模式

结构型模式之 组合模式 前言 模式定义 模式结构 UML类图 适用场景 在以下情况下可以使用组合模式 看看大神怎么用 高清有码 总结 感谢结构型模式之 组合模式前言对于树形结构,在代码中有容器节点和叶...
  • u012984054
  • u012984054
  • 2016年08月18日 01:00
  • 814

对于组合模式的理解

今天看了组合模式 ,多用于树形结构 定义:  组合模式(Composite Pattern):组合多个对象形成树形结构以表示具有“整体—部分”关系的层次结构。组合模式对单个对象(即叶子对象)和组合对象...
  • AlbertFly
  • AlbertFly
  • 2016年06月16日 12:14
  • 3260

树形结构的处理——组合模式(四):透明组合模式与安全组合模式

11.4 透明组合模式与安全组合模式通过引入组合模式,Sunny公司设计的杀毒软件具有良好的可扩展性,在增加新的文件类型时,无须修改现有类库代码,只需增加一个新的文件类作为AbstractFile类的...
  • will130
  • will130
  • 2016年01月19日 11:06
  • 594

JAVA设计模式初探之组合模式

先看看组合模式的定义吧:“将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。”    就拿剪发办卡的事情来分析一下吧。    首先,一张卡可...
  • jason0539
  • jason0539
  • 2014年03月31日 08:40
  • 22690

【js设计模式笔记---组合模式】

组合模式 组合模式是一种专为创建Web上动态用户界面而量身定制的模式。使用这种模式,可以用一条命令在多个对象上激发复杂的或递归的行为。 组合模式为操劳过度的javascript程序员带来了两大的好处 ...
  • pigpigpig4587
  • pigpigpig4587
  • 2014年05月19日 15:42
  • 849

设计模式——组合模式+案例

先给个代码地址吧 https://github.com/yyfyyf1994/knowledge/tree/master/src/yyf/designpatterns/composite ...
  • qq_18860653
  • qq_18860653
  • 2016年11月21日 16:51
  • 700

组合模式(Composite Pattern)(一):组合模式介绍

一、意图 将对象组合成树形结构以表示“部分-整体”的层次结构。Composite模式使得用户对单个对象和组合对象的使用具有一致性。 二、适用性 • 你想表示对象的...
  • jialinqiang
  • jialinqiang
  • 2013年05月20日 22:30
  • 3584

设计模式--组合模式--商品排序案例

所有的例子均来源与实际开发项目 本节介绍组合模式的使用–商品结果排序评分系统...
  • yangxy81118
  • yangxy81118
  • 2014年08月20日 19:48
  • 2935

深入浅出组合模式

一、引子在大学的数据结构这门课上,树是最重要的章节之一。还记得树是怎么定义的吗?树(Tree)是n(n≥0)个结点的有限集T,T为空时称为空树,否则它满足如下两个条件:(1)    有且仅有一个特定的...
  • ai92
  • ai92
  • 2005年02月23日 10:33
  • 14463

Java 设计模式——组合模式

有时我们可能会被要求处理一个层级结构明显的对象,比如上下级的公司员工、比如层级嵌套的文件夹,还有丰富多彩的美食菜单。可是,我们可能要屡试不爽地编写深度搜索代码、要小心翼翼地编写递归逻辑。现在你可以忘掉...
  • u013761665
  • u013761665
  • 2016年05月17日 17:39
  • 7810
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:组合模式
举报原因:
原因补充:

(最多只允许输入30个字)