浅谈设计模式--组合模式(Composite Pattern)
组合模式(Composite Pattern)
组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性。简单来说,就是可以像使用一个对象那样,来使用一组对象(The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object.),最后达到用户和这一组对象的解耦。请看下图:
Component,就是对Leaf和Composite的抽象,是Leaf和Composite必须实现的接口(或者抽象类)
这单个对象,就是Composite,提供操作Leaf的方法和实现所用Component的方法。
这一组对象,就是Leaf。
例子如下:
/** Component **/
interface Graphic { //Prints the graphic
public void print(); } /** Leaf **/
public class CircleLeaf implements Graphic { public void print() {
System.out.println("Circle...");
} } /** Leaf **/
public class RectangleLeaf implements Graphic{ public void print() {
System.out.println("Rectangle...");
}
}
/** Composite **/
public class GraphicComposite implements Graphic{ private List<Graphic> childGraphics = new ArrayList<Graphic>(); public void print() {
//key method. Loop and call Leaf method.
for(Graphic g : childGraphics){
g.print();
}
} // manipulate Leaf
public void add(Graphic g){
childGraphics.add(g);
}
public void remove(Graphic g){
childGraphics.remove(g);
} }
这样,客户端就不需要操作每一个Leaf,直接通过GraphicComposite可以操作所有的Leaf:
public class ClientSide { public static void main(String[] args) { GraphicComposite graphic = new GraphicComposite();
graphic.add(new CircleLeaf());
graphic.add(new RectangleLeaf()); graphic.print();
}
}
最后,最开始看到的这个模式的使用,是在我参加的一个开源项目里。当时候觉得设计得不错,没想到是Composite模式。之后,自己也在项目中使用过,特此写下此贴来总结一下。
参考:
http://en.wikipedia.org/wiki/Composite_pattern
http://www.cnblogs.com/peida/archive/2008/09/09/1284686.html
浅谈设计模式--组合模式(Composite Pattern)的更多相关文章
- 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释
组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...
- 设计模式 -- 组合模式 (Composite Pattern)
定义: 对象组合成部分整体结构,单个对象和组合对象具有一致性. 看了下大概结构就是集团总公司和子公司那种层级结构. 角色介绍: Component :抽象根节点:其实相当去总公司,抽象子类共有的方法: ...
- C#设计模式——组合模式(Composite Pattern)
一.概述 在软件开发中,我们往往会遇上类似树形结构的对象体系.即某一对象既可能在树形结构中作为叶节点存在,也可能作为分支节点存在.比如在文件系统中,文件是作为叶节点存在,而文件夹就是分支节点.在设计这 ...
- 乐在其中设计模式(C#) - 组合模式(Composite Pattern)
原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...
- 设计模式系列之组合模式(Composite Pattern)——树形结构的处理
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 二十四种设计模式:组合模式(Composite Pattern)
组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...
- 【设计模式】组合模式 Composite Pattern
树形结构是软件行业很常见的一种结构,几乎随处可见, 比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...
- python 设计模式之组合模式Composite Pattern
#引入一 文件夹对我们来说很熟悉,文件夹里面可以包含文件夹,也可以包含文件. 那么文件夹是个容器,文件夹里面的文件夹也是个容器,文件夹里面的文件是对象. 这是一个树形结构 咱们生活工作中常用的一种结构 ...
- 设计模式-12组合模式(Composite Pattern)
1.模式动机 很多时候会存在"部分-整体"的关系,例如:大学中的部门与学院.总公司中的部门与分公司.学习用品中的书与书包.在软件开发中也是这样,例如,文件系统中的文件与文件夹.窗体 ...
随机推荐
- Linux high memory 学习总结
在free命令中有个参数l,它表示 show detailed low and high memory statistics.其实最先是对High Memory总是为零有些不解(Linux是64为). ...
- 让你脱离google不能访问的烦恼
大陆封了google已有20多天了,给开发者带来了许多不便.只需两步让你的google可以使用: 1.设置hosts: 访问:https://git.oschina.net/kawaiiushio/m ...
- SQL Server ---(CDC)监控表数据(转译)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现过程(Realization) 补充说明(Addon) 参考文献(References) ...
- 0015 Java学习笔记-集合-TreeMap集合
主要的方法 构造方法: TreeMap(); TreeMap(Comparator<?super K> comparator); TreeMap(Map<? extends K,? ...
- Servlet/JSP-07 Session应用
Session应用 一. 避免表单重复提交 1. 表单重复提交的情况 ①在表单提交到一个 Servlet,而 Servlet 又通过请求转发的方式响应了一个 JSP 或者 HTML 页面,此时浏览器地 ...
- Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法
Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法 You can extract all the ...
- Makefile关键字
@ makefile会把将要执行的命令行在命令执行前输出到屏幕上,使用@可以避免显示出命令本身 若@ echo 正在编译-则$make显示正在编译- 若echo 正在编译-则$make显示: echo ...
- WIN32 API编程之 透明static
createwindow可以直接创建一个staitc,但这个static是不透明的,如果我们把窗口背景设置为GRAY_BRUSH,则static会很明显的有一个白色背景,一般来说这样肯定很难看. 可以 ...
- PCI在linux系统中注册与注销示例
1. pci_driver结构struct pci_driver { struct list_head node; const char *name; const struct pc ...
- 报表session与应用session常识普及
1. 报表session与应用session 报表集成到项目中可能会有一个疑问就是系统应用和报表应用在一个web服务器下,那系统session和报表session是不是一个session呢?如果不是那 ...