一、什么是组合模式

  Composite模式也叫组合模式,是构造型的设 计模式之一。通过递归手段来构造树形的对象结 构,并可以通过一个对象来访问整个对象树。

二、组合模式的结构

三、组合模式的角色和职责

  Component (树形结构的节点抽象)

  - 为所有的对象定义统一的接口(公共属性,行为等的定义)

   - 提供管理子节点对象的接口方法

  - [可选]提供管理父节点对象的接口方法

  Leaf (树形结构的叶节点)

  Component的实现子类

  Composite(树形结构的枝节点)

  Component的实现子类

文件和目录的父类

 import java.util.List;

 /*
* 文件节点抽象(是文件和目录的父类)
*/
public interface IFile { //显示文件或者文件夹的名称
public void display(); //添加
public boolean add(IFile file); //移除
public boolean remove(IFile file); //获得子节点
public List<IFile> getChild();
}

文件

 import java.util.List;

 //文件
public class File implements IFile {
private String name; public File(String name) {
this.name = name;
} public void display() {
System.out.println(name);
} public List<IFile> getChild() {
return null;
} public boolean add(IFile file) {
return false;
} public boolean remove(IFile file) {
return false;
}
}

文件夹

 import java.util.ArrayList;
import java.util.List; //文件夹
public class Folder implements IFile{
private String name;
private List<IFile> children; public Folder(String name) {
this.name = name;
children = new ArrayList<IFile>();
} public void display() {
System.out.println(name);
} public List<IFile> getChild() {
return children;
} public boolean add(IFile file) {
return children.add(file);
} public boolean remove(IFile file) {
return children.remove(file);
}
}

测试

 import java.util.List;

 public class MainClass {
public static void main(String[] args) {
//C盘
Folder rootFolder = new Folder("C:");
//beifeng目录
Folder beifengFolder = new Folder("beifeng");
//beifeng.txt文件
File beifengFile = new File("beifeng.txt"); rootFolder.add(beifengFolder);
rootFolder.add(beifengFile); //ibeifeng目录
Folder ibeifengFolder = new Folder("ibeifeng");
File ibeifengFile = new File("ibeifeng.txt");
beifengFolder.add(ibeifengFolder);
beifengFolder.add(ibeifengFile); Folder iibeifengFolder = new Folder("iibeifeng");
File iibeifengFile = new File("iibeifeng.txt");
ibeifengFolder.add(iibeifengFolder);
ibeifengFolder.add(iibeifengFile); displayTree(rootFolder,0); } public static void displayTree(IFile rootFolder, int deep) {
for(int i = 0; i < deep; i++) {
System.out.print("--");
}
//显示自身的名称
rootFolder.display();
//获得子树
List<IFile> children = rootFolder.getChild();
//遍历子树
for(IFile file : children) {
if(file instanceof File) {
for(int i = 0; i <= deep; i++) {
System.out.print("--");
}
file.display();
} else {
displayTree(file,deep + 1);
}
}
}
}

13组合模式Composite的更多相关文章

  1. 组合模式/composite模式/对象结构型模式

    组合模式/composite模式/对象结构型 意图 将对象组合成树形结构以表示"整体--部分"的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 动机 C ...

  2. 浅谈设计模式--组合模式(Composite Pattern)

    组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...

  3. 二十四种设计模式:组合模式(Composite Pattern)

    组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...

  4. 设计模式(七)组合模式Composite(结构型)

    设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 ...

  5. 乐在其中设计模式(C#) - 组合模式(Composite Pattern)

    原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...

  6. 【设计模式】组合模式 Composite Pattern

    树形结构是软件行业很常见的一种结构,几乎随处可见,  比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...

  7. 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释

    组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...

  8. 设计模式系列之组合模式(Composite Pattern)——树形结构的处理

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  9. C#设计模式——组合模式(Composite Pattern)

    一.概述 在软件开发中,我们往往会遇上类似树形结构的对象体系.即某一对象既可能在树形结构中作为叶节点存在,也可能作为分支节点存在.比如在文件系统中,文件是作为叶节点存在,而文件夹就是分支节点.在设计这 ...

随机推荐

  1. Java基础之理解Annotation

    一.概念 Annontation是Java5开始引入的新特征.中文名称一般叫注解.它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata)与程序元素(类.方法.成员变量等)进行关 ...

  2. Linux下如何查看系统启动时间和运行时间(转)

    1.uptime命令输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.00 2.查看/proc/uptime文件计算 ...

  3. makefile中的shell编程注意点

    参考:http://blog.csdn.net/wanglang3081/article/details/49423105 (1)Makefile本质上来讲也是shell脚本,即每条command都是 ...

  4. jconsole工具使用

    Jconsole,Java Monitoring and Management Console. Jconsole是JDK自带的监控工具,在JDK/bin目录下可以找到.它用于连接正在运行的本地或者远 ...

  5. Cassandra的数据模型的理解

           Cassandra属于NoSQL数据库,NoSQL和传统关系型数据库不同,NOSQL偏好数据冗余,因为NoSQL一般无法做表关联查询. (1) keySpace 基本上可以将Keyspa ...

  6. [Canvas]Bombman v1.00

    爆破小人Canvas版,请点此下载,并用浏览器打开试玩. 图例: 源码: <!DOCTYPE html> <html lang="utf-8"> <m ...

  7. 微软BI 之SSIS 系列 - 平面文件格式的区别(Delimited,Fixed width,Ragged Right, Fixed width ...)

    开篇介绍 SSIS 中处理文件,一般在描述输出平面文件格式的时候通常会出现以下几种选项: Delimited - 默认输出列使用逗号分隔,也可以选择其它的诸如 | ,或者 Tab 等. Fixed W ...

  8. docker-compose中启动镜像失败的问题

    http://blog.csdn.net/boling_cavalry/article/details/79050451 解决docker-compose启动镜像失败的问题: 原文地址:http:// ...

  9. (98)Address already in use: make_sock: could not bind to address 80 [resolved] (2012-10-11 09:04)

    以前遇到一个问题: sudo /etc/init.d/apache2 start * Starting web server apache2 apache2: Could not reliably d ...

  10. mysql如何处理亿级数据,第一个阶段——优化SQL语句

    1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...