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)
一.概述 在软件开发中,我们往往会遇上类似树形结构的对象体系.即某一对象既可能在树形结构中作为叶节点存在,也可能作为分支节点存在.比如在文件系统中,文件是作为叶节点存在,而文件夹就是分支节点.在设计这 ...
随机推荐
- A - K进制下的大数
https://vjudge.net/contest/218366#problem/A 中间溢出,注意求模. #include<iostream> #include<cstdio&g ...
- ${pageContext.request.contextPath}无法解析
摘要 突然出现无法解析${pageContext.request.contextPath}的问题,在点击<a href="${pageContext.request.contextPa ...
- JavaScript数组所有API全解密
全文共13k+字,系统讲解了JavaScript数组的各种特性和API. 数组是一种非常重要的数据类型,它语法简单.灵活.高效. 在多数编程语言中,数组都充当着至关重要的角色,以至于很难想象没有数组的 ...
- How to change the implementation (detour) of an externally declared function
原文地址:http://stackoverflow.com/questions/6905287/how-to-change-the-implementation-detour-of-an-extern ...
- Scala:Functions and Closures
object Functions { def main(args: Array[String]) { // 本地函数 def localFun(msg: String) = println(msg) ...
- css3 transition属性实现3d动画效果
transition属性是一个很强大的3d动画属性,我动手试了一下,很多在网上很火的网页动画都可以用这个属性实现,只能说这个属性是在是太强大啦,本人在学习次属性之后才知道自己对css3的认识还是偏少, ...
- MySQL架构与业务总结图
MySQL架构与业务总结图如下:
- 【Little Demo】从简单的Tab标签到Tab图片切换
Tab标签切换效果是比较流行的一种网站页面布局,视觉表现为美观大方,通过标签展示内容.目前在各大网站都有存在这种效果.例如:淘宝的黄金位置使用Tab标签切换效果,网易新闻等. 1.简单的 Tab 标签 ...
- Vim命令相关
在shell中,记住一些常用的vim命令,会在操作时候事半功倍. 光标移动 h,j,k,l,h #表示往左,j表示往下,k表示往右,l表示往上 Ctrl f #上一页 Ctrl b #下一页 w, e ...
- Spark机器学习(8):LDA主题模型算法
1. LDA基础知识 LDA(Latent Dirichlet Allocation)是一种主题模型.LDA一个三层贝叶斯概率模型,包含词.主题和文档三层结构. LDA是一个生成模型,可以用来生成一篇 ...