一天一个设计模式——Composite组合模式
一、模式说明
能够使容器与内容物具有一致性,创造出递归结构的模式就是Composite组合模式。
举个例子:计算机中的文件系统中有文件和文件夹的概念,我们知道,文件夹可以包含文件,也可以包含子文件夹,子文件夹中又可以包含文件和文件夹。如果将文件和文件夹都看作“目录条目(Directory Entry)”,那么文件系统就形成了一个递归的容器,这就是一种组合模式。
更多例子,在Windows操作系统中,一个窗口中可以包含子窗口,从而形成容器的递归;一条宏命令又可以包含另一个宏命令;一个文章章节又可以包含多个子章节。。。这种具有树状结构的数据都适用于组合模式。
二、模式类图

三、代码示例
在示例程序中,我们创建一个Entry目录条目类,并让File文件类和Dictory文件夹类去继承它。
1、FileTreatementException类
package com.designpattern.cn.Entry;
public class FileTreatementException extends RuntimeException {
public FileTreatementException(){
}
public FileTreatementException(String msg){
super(msg);
}
}
2、Entry条目类
package com.designpattern.cn.Entry;
public abstract class Entry {
public abstract String getName();
public abstract int getSize();
public Entry add(Entry entry) throws FileTreatementException{
throw new FileTreatementException();
}
public void PrintList(){
printList("");
}
protected abstract void printList(String prefix);
public String toString(){
return getName() + " (" + getSize() + ")";
}
}
3、File文件类
package com.designpattern.cn.Entry;
public class File extends Entry {
private String name;
private int size;
public File(String name, int size){
this.name = name;
this.size = size;
}
public String getName(){
return name;
}
public int getSize(){
return size;
}
protected void printList(String prefix){
System.out.println(prefix + "/" + this);
}
}
4、Directory目录类
package com.designpattern.cn.Entry; import java.util.ArrayList;
import java.util.Iterator; public class Directory extends Entry {
private String name;
private ArrayList directory = new ArrayList();
public Directory(String name){
this.name = name;
}
public String getName(){
return name;
}
public int getSize(){
int size = 0;
Iterator it = directory.iterator();
while (it.hasNext()){
Entry entry = (Entry) it.next();
size += entry.getSize();
}
return size;
}
public Entry add(Entry entry){
directory.add(entry);
return this;
}
protected void printList(String prefix){
System.out.println(prefix + "/" + this);
Iterator it = directory.iterator();
while (it.hasNext()){
Entry entry = (Entry) it.next();
entry.printList(prefix + "/" + name);
}
}
}
5、Main方法类和运行结果
package com.designpattern.cn.Entry;
public class Main {
public static void main(String[] args) {
try {
System.out.println("Making root entries...");
Directory rootdir = new Directory("root");
Directory bindir = new Directory("bin");
Directory tmpdir = new Directory("tmp");
Directory usrdir = new Directory("usr");
rootdir.add(bindir);
rootdir.add(tmpdir);
rootdir.add(usrdir);
bindir.add(new File("vi", 10000));
bindir.add(new File("latex", 20000));
rootdir.printList("");
}catch (FileTreatementException e){
e.printStackTrace();
}
}
}

四、模式中的角色
- Leaf树叶角色:该角色中不能再放入其他对象,如示例中的File类;
- Composite复合物类:在其中可以放入Leaf和Composite复合物,如示例程序中的Directory类;
- Component类:使Leaf和Composite角色具有一致性的角色,如示例中的Entry类;
- Client客户角色:使用复合模式的对象。
五、相关的设计模式
- Command模式:Command模式编写宏命令时会使用Composite模式;
- Vistor访问者模式:可以使用访问者模式访问Composite中的递归结构;
- Decorator装饰者模式:Composite模式通过Component角色使Composite和Leaf具有一致性,Decorator模式使装饰框和内容具有一致性;
一天一个设计模式——Composite组合模式的更多相关文章
- C++设计模式-Composite组合模式
Composite组合模式作用:将对象组合成树形结构以表示“部分-整体”的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. UML图如下: 在Component中声明所有用来 ...
- [C++设计模式] composite 组合模式
组合(Composite)模式的其他翻译名称也非常多,比方合成模式.树模式等等.在<设计模式>一书中给出的定义是:将对象以树形结构组织起来,以达成"部分-总体"的层次结 ...
- C#设计模式(10)——组合模式(Composite Pattern)
一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...
- 乐在其中设计模式(C#) - 组合模式(Composite Pattern)
原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...
- C#设计模式(10)——组合模式(Composite Pattern)(转)
一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...
- 设计模式08: Composite 组合模式(结构型模式)
Composite 组合模式(结构型模式) 对象容器的问题在面向对象系统中,我们常会遇到一类具有“容器”特征的对象——即他们在充当对象的同时,又是其他对象的容器. public interface I ...
- C#设计模式:组合模式(Composite Pattern)
一,C#设计模式:组合模式(Composite Pattern) using System; using System.Collections.Generic; using System.Linq; ...
- JavaScript设计模式之----组合模式
javascript设计模式之组合模式 介绍 组合模式是一种专门为创建Web上的动态用户界面而量身制定的模式.使用这种模式可以用一条命令在多个对象上激发复杂的或递归的行为.这可以简化粘合性代码,使其更 ...
- 十一、Composite 组合模式
原理: 代码清单 Entity public abstract class Entry { public abstract String getName(); public abstract int ...
随机推荐
- PAT (Advanced Level) 1144~1147:1145Hash二次探查 1146拓扑排序 1147堆
1144 The Missing Number(20 分) 题意:给定N个数的序列,输出不在序列中的最小的正整数. 分析: 1.给定的N个数可能为正,可能为负,可能重复. 2.由于N≤105,所 ...
- NPOI读取excel 空行
if (sheet.GetRow(i) != null) 每行判断一下,避免出错.真是蛋疼.
- 吴裕雄--天生自然java开发常用类库学习笔记:System类
public class SystemDemo01{ public static void main(String args[]){ long startTime = System.currentTi ...
- P1051复数乘法
P1051复数乘法 转跳点:
- C# OBJ模型解析的封装(网上看到的保留一份)
/// <author>Lukas Eibensteiner</author> /// <date>19.02.2013</date> /// < ...
- springcloud--zuul(过滤器)
在zuul添加过滤器 新建类继承ZuulFilter类. public class MyFilter extends ZuulFilter{ //是否需要过滤 @Override public boo ...
- SpringBoot#自定义配置的封装
_震惊,开局 不可避免的需要弄一些自定义的配置. 要点: 1. 把配置项都写出来,分析层次关系:2. 抽象成bean与bean之间的关系,写出bean对应的类,这时候配置项对应了bean的属性,属性可 ...
- Biu一Biu--GDB
gcc常见编译选项 ** -c **:只激活预处理.编译和汇编,也就是生成obj文件 ** -S **:只激活处理和编译,把文件编译成汇编代码 ** -o **:定制目标名称,缺省的时候编译出来的可执 ...
- 052-PHP输出多个参数
<?php $x=5; //初始化两个变量 $y=10; echo $x,$y,"<br />$x+$y=",$x+$y; //输出多个参数 ?>
- 第二阶段scrum-6
1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 消息收发功能正在制作