设计模式 -- 组合模式 (Composite Pattern)
定义:
对象组合成部分整体结构,单个对象和组合对象具有一致性。
看了下大概结构就是集团总公司和子公司那种层级结构。
实现树状嵌套结构。
角色介绍:
Component :抽象根节点:其实相当去总公司,抽象子类共有的方法;
Composite :相当于总公司的智能部门,也分管子公司,通过集合存储子节点对象,提供增删获取子节点对象的方法;
leaf:子节点,相当于集团子公司,总公司具有的智能,子公司也具有,因此子节点具有总节点拥有的所有抽象方法以及提供给子类的方法。
Client:通过抽象跟节点操作子节点的对象。
public abstract class Component {
protected String name;
public Component(String name) {
this.name = name;
}
public abstract void doSomething();
}
import java.util.ArrayList;
import java.util.List; /**
* 具体枝干节点
* Created by Administrator on 2016/9/3.
*/
public class Composite extends Component {
private List<Component> components = new ArrayList<>(); public Composite(String name) {
super(name);
} //新增子公司
public void addChild(Component component) {
components.add(component);
} //撤除子公司
public void remove(Component component) {
components.remove(component);
} //获取某个子公司对象信息
public void getChild(int index) {
components.get(index);
} @Override
public void doSomething() {
System.out.println(name);
for (int i = 0; i < components.size(); i++) {
components.get(i).doSomething();
}
}
}
public class Leaf extends Component {
public Leaf(String name) {
super(name);
}
//相当于输出子公司名称
@Override
public void doSomething() {
System.out.println(name);
}
}
运行结果:
public class Test {
public static void main(String[] args) {
Composite composite = new Composite("composite");
Composite composite1 = new Composite("composite1");
Composite composite2 = new Composite("composite1");
Leaf leaf = new Leaf("leaf1");
Leaf leaf2 = new Leaf("leaf2");
composite1.addChild(leaf);
composite2.addChild(leaf2);
composite.addChild(composite1);
composite.addChild(composite2);
composite.doSomething();
}
}
实际项目中的使用:
- 比如对sd卡文件和文件夹的操作上其实就可以使用该设计模式:
- 定义一个抽象类表示文件和文件夹,包含对文件增加,删除,遍历的抽象方法;
- 文件夹类继承自1中的抽象类,并实现抽象类中的抽象方法;
- 文件类继承自1中的抽象类,并实现抽象类中的抽象方法;
android源码里面使用到组合模式就是View和ViewGroup的嵌套组合。
还是很粗糙,以后使用过程中慢慢体会吧
设计模式 -- 组合模式 (Composite Pattern)的更多相关文章
- 浅谈设计模式--组合模式(Composite Pattern)
组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...
- 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释
组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...
- 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.模式动机 很多时候会存在"部分-整体"的关系,例如:大学中的部门与学院.总公司中的部门与分公司.学习用品中的书与书包.在软件开发中也是这样,例如,文件系统中的文件与文件夹.窗体 ...
随机推荐
- intellij idea 学习
intellij idea是一个java的ide.由jetbrain开发. intellij idea快捷键 快捷键 说明 备注 Ctrl+N 快速导航到类 类似于Resharper中的C ...
- @Html.CheckBoxFor为何输出两种控件
在MVC中当使用@Html.CheckBoxFor时表单上会产生两种控件checkbox和hidden,比如: @Html.CheckBoxFor(model => model.IsTop) 对 ...
- 模块化开发AraeRegistration
.NET/ASP.NET MVC(模块化开发AraeRegistration) 阅读目录: 1.开篇介绍 2.AreaRegistration注册路由(传递路由上下文进行模块化注册) 1]开篇介绍 A ...
- [转]iOS Assembly Tutorial: Understanding ARM
iOS Assembly Tutorial: Understanding ARM Do you speak assembly? When you write Objective-C code, it ...
- 图解Javascript引用类型之数组
以图说事明理,恰当时候会事半功陪.今天我就尝试着用图的方式讲讲“JavaScript引用类型之数组”.望更多童鞋给我反馈! 好东西分享给大家,但要尊重事实!!!因此特别说明:本图非我本人亲自所作,乃我 ...
- Asp.net 插入或更改查询字符串
string InsertOrUpdateQueryStringItem(string key, string value) { if (Request.QueryString.HasKeys()) ...
- easyui tree 判断是否是叶子节点
<input class="add" id="add" style="display: none" type="submit ...
- java正则表达式验证汉字
统计指定内容的汉字个数: String str = "北京欢迎你 hello welcome!"; int count=0; Pattern pattern = Pattern.c ...
- MyEclipse修改默认的workspace路径
在此只提供一个自己认为可行的办法(已验证可行) 已MyEclipse8.5为例 打开安装路径C:\Program Files\Genuitec\MyEclipse 8.5\configuration下 ...
- 记那一次C++开发电话面试
题目一: linux进程间通信(IPC)有几种方式,下面将将简单的简述一下: 一.管道(pipe) 管道是Linux支持的最初IPC方式,管道可分为无名管道,有名管道等. (一)无名管道,它具有几个特 ...