13组合模式Composite
一、什么是组合模式
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的更多相关文章
- 组合模式/composite模式/对象结构型模式
组合模式/composite模式/对象结构型 意图 将对象组合成树形结构以表示"整体--部分"的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 动机 C ...
- 浅谈设计模式--组合模式(Composite Pattern)
组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...
- 二十四种设计模式:组合模式(Composite Pattern)
组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...
- 设计模式(七)组合模式Composite(结构型)
设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 ...
- 乐在其中设计模式(C#) - 组合模式(Composite Pattern)
原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...
- 【设计模式】组合模式 Composite Pattern
树形结构是软件行业很常见的一种结构,几乎随处可见, 比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...
- 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释
组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...
- 设计模式系列之组合模式(Composite Pattern)——树形结构的处理
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- C#设计模式——组合模式(Composite Pattern)
一.概述 在软件开发中,我们往往会遇上类似树形结构的对象体系.即某一对象既可能在树形结构中作为叶节点存在,也可能作为分支节点存在.比如在文件系统中,文件是作为叶节点存在,而文件夹就是分支节点.在设计这 ...
随机推荐
- Java基础-多线程-③线程同步之synchronized
使用线程同步解决多线程安全问题 上一篇 Java基础-多线程-②多线程的安全问题 中我们说到多线程可能引发的安全问题,原因在于多个线程共享了数据,且一个线程在操作(多为写操作)数据的过程中,另一个线程 ...
- java连接数据库插入数据中文乱码
解决方案: jdbc连接数据库,向表中插入中文查看数据乱码:修改数据库连接url为jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8 注意 ...
- Revit API 创建带箭头的标注
[Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class cmd : ...
- centos 终端界面代理设置
一.centos自带界面设置代理 1. 界面设置 squid默认代理端口3128. 2. firefox设置 设置 -> 局域网设置 -> ip:port / username:passw ...
- app v1界面
- 版本适配 sdk version MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- oracle获取今年在内的前几年、后几年
前几年 select to_char(sysdate, 'yyyy') - level + 1 years from dual connect by level <= num num:即想获取几 ...
- C# 怎么让winform程序中的输入文本框保留上次的输入
选中TextBox控件,在属性窗格中找到(ApplicationSettings),然后设置它. 绑定配置文件 private Settings settings = new Settings(); ...
- Atitit 验证 数字验证 非空验证的最佳算法 h5
Atitit 验证 数字验证 非空验证的最佳算法 h5 <td><select class="searchBox-select" style=" ...
- 最简单的基于FFmpeg的AVfilter样例(水印叠加)
===================================================== 最简单的基于FFmpeg的AVfilter样例系列文章: 最简单的基于FFmpeg的AVfi ...