设计模式-12组合模式(Composite Pattern)
1.模式动机
很多时候会存在“部分-整体”的关系,例如:大学中的部门与学院、总公司中的部门与分公司、学习用品中的书与书包。在软件开发中也是这样,例如,文件系统中的文件与文件夹、窗体程序中的简单控件与容器控件等。对这些简单对象与复合对象的处理,如果用组合模式来实现会很方便。
2.模式定义
组合(Composite)模式的定义:有时又叫作部分-整体模式,它是一种将对象组合成树状的层次结构的模式,用来表示“部分-整体”的关系,使用户对单个对象和组合对象具有一致的访问性。
属于:结构型模式。
组合模式分为透明式的组合模式和安全式的组合模式。
3.模式结构
组合模式包含以下主要角色:
- 抽象构件(Component)角色:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。
- 树叶构件(Leaf)角色:是组合中的叶节点对象,它没有子节点,用于实现抽象构件角色中 声明的公共接口。
- 树枝构件(Composite)角色:是组合中的分支节点对象,它有子节点。它实现了抽象构件角色中声明的接口,它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。
透明式组合方式
在该方式中,由于抽象构建声明了所有子类中的全部方法,所以客户端无需区别树叶对象和树枝对象,对客户端来说是透明的。
缺点是:树叶构建本来没有 add()、remove() 及 getChild() 等方法,却要实现它们(空实现或抛异常),这样会带来一些安全性问题。
安全式组合方式
在该方式中,将管理自购件的方法移到树枝构件中,抽象构件和树叶构件没有对子对象的管理方法,这样就避免了上一种方式的安全性问题。
但由于叶子和分支有不同的接口,客户端在调用时要知道树叶对象和树枝对象的存在,所以失去了透明性。
4.模式代码
透明式组合方式
# 抽象构件
public abstract class Component {
protected String name;
public Component(String name) {
this.name = name;
}
public abstract String name();
public boolean addChild(Component component) {
throw new UnsupportedOperationException("addChild not supported!");
}
public boolean removeChild(Component component) {
throw new UnsupportedOperationException("removeChild not supported!");
}
public Component getChild(int index) {
throw new UnsupportedOperationException("getChild not supported!");
}
}
# 树枝构件
public class Composite extends Component {
private List<Component> componentList;
public Composite(String name) {
super(name);
componentList = new ArrayList<>();
}
@Override
public String name() {
StringBuilder builder = new StringBuilder(this.name);
for (Component component : componentList) {
builder.append("\n");
builder.append(component.name());
}
return builder.toString();
}
@Override
public boolean addChild(Component component) {
return componentList.add(component);
}
@Override
public boolean removeChild(Component component) {
return componentList.remove(component);
}
@Override
public Component getChild(int index) {
return componentList.get(index);
}
}
# 树叶构件
public class Leaf extends Component {
public Leaf(String name) {
super(name);
}
@Override
public String name() {
return name;
}
}
# Client
public class Client {
public static void main(String[] args) {
// 根节点
Component root = new Composite("root");
// 树枝节点
Component branchA = new Composite("---branchA");
Component branchB = new Composite("------branchB");
// 树叶节点
Component leafA = new Leaf("------leafA");
Component leafB = new Leaf("---------leafB");
Component leafC = new Leaf("---leafC");
root.addChild(branchA);
root.addChild(leafC);
branchA.addChild(leafA);
branchA.addChild(branchB);
branchB.addChild(leafB);
// 叶子节点不支持操作
// leafC.addChild(null);
String result = root.name();
System.out.println(result);
}
}
按照透明式的字面意思,这里应该没有 Leaf 类,只有树枝构件,树枝构件既可以作为树枝构件也可以作为树叶构件,所以对客户端来说是透明的。
安全式组合方式
安全式组合方式就是将透明式组合方式 Component 中的 add、remove 等方法放在树枝节点中,抽象构件中之定义树枝构件和树叶构件中公共的部分。
# 抽象构件
public abstract class Component {
protected String name;
public Component(String name) {
this.name = name;
}
public abstract String name();
}
# 树枝构件
public class Composite extends Component {
private List<Component> componentList;
public Composite(String name) {
super(name);
componentList = new ArrayList<>();
}
@Override
public String name() {
StringBuilder builder = new StringBuilder(this.name);
for (Component component : componentList) {
builder.append("\n");
builder.append(component.name());
}
return builder.toString();
}
public boolean addChild(Component component) {
return componentList.add(component);
}
public boolean removeChild(Component component) {
return componentList.remove(component);
}
public Component getChild(int index) {
return componentList.get(index);
}
}
# 树叶构件
public class Leaf extends Component {
public Leaf(String name) {
super(name);
}
@Override
public String name() {
return name;
}
}
# Client
public class Client {
public static void main(String[] args) {
// 根节点 这里改为 Composite
Composite root = new Composite("root");
// 树枝节点 这里改为 Composite
Composite branchA = new Composite("---branchA");
Composite branchB = new Composite("------branchB");
// 树叶节点
Leaf leafA = new Leaf("------leafA");
Leaf leafB = new Leaf("---------leafB");
Leaf leafC = new Leaf("---leafC");
root.addChild(branchA);
root.addChild(leafC);
branchA.addChild(leafA);
branchA.addChild(branchB);
branchB.addChild(leafB);
// 叶子节点不支持操作
// leafC.addChild(null);
String result = root.name();
System.out.println(result);
}
}
树叶构件时,客户端需要区分树枝构件(Composite)和树叶构件(Leaf),所以失去了透明性。按照定义,透明式模式中就不应该有 Leaf 类的定义。
这样的好处是 接口定义职责清晰,符合设计模式的 单一职责原则 和 接口隔离原则。
5.总结
优点:
- 组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,这简化了客户端代码;
- 更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足“开闭原则”;
缺点:
- 设计较复杂,客户端需要花更多时间理清类之间的层次关系;
- 不容易限制容器中的构件;
- 不容易用继承的方法来增加构件的新功能;
场景:
- 在需要表示一个对象整体与部分的层次结构的场合。
- 要求对用户隐藏组合对象与单个对象的不同,用户可以用统一的接口使用组合结构中的所有对象的场合。
设计模式-12组合模式(Composite Pattern)的更多相关文章
- 乐在其中设计模式(C#) - 组合模式(Composite Pattern)
原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...
- 二十四种设计模式:组合模式(Composite Pattern)
组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...
- 【设计模式】组合模式 Composite Pattern
树形结构是软件行业很常见的一种结构,几乎随处可见, 比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...
- python 设计模式之组合模式Composite Pattern
#引入一 文件夹对我们来说很熟悉,文件夹里面可以包含文件夹,也可以包含文件. 那么文件夹是个容器,文件夹里面的文件夹也是个容器,文件夹里面的文件是对象. 这是一个树形结构 咱们生活工作中常用的一种结构 ...
- 设计模式系列之组合模式(Composite Pattern)——树形结构的处理
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 浅谈设计模式--组合模式(Composite Pattern)
组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...
- 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释
组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...
- java_设计模式_组合模式_Composite Pattern(2016-08-12)
概念: 组合模式(Composite Pattern)将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性. 有时候又叫做部分-整体模式,它使我们树 ...
- C#设计模式——组合模式(Composite Pattern)
一.概述 在软件开发中,我们往往会遇上类似树形结构的对象体系.即某一对象既可能在树形结构中作为叶节点存在,也可能作为分支节点存在.比如在文件系统中,文件是作为叶节点存在,而文件夹就是分支节点.在设计这 ...
随机推荐
- mplayer 的安装步骤
编译mplayer: make distclean ./configure --disable-png --disable-gif //加后面的是因为编译时出错了,也可以直接 ./con ...
- 十一、linux-mysql的多种日志和引擎文件实战
一.多种日志 mysql生成或者相关联的日志文件种类繁多,本节重点关注与mysql数据库服务相关的几类日志文件. 错误日志(error log) :记录mysql服务进程mysqld在启动/关闭或者 ...
- 关于JDBC、JdbcTemplate使用遇到的坑
1.如果数据源是oracle(mysql结尾是可以使用";"的),sql字符串中结尾处禁止使用分号";",不然会报错:java.sql.SQLException ...
- [LC] 314. Binary Tree Vertical Order Traversal
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- Invert Binary Tree(easy)
1.直接把递归把左右子树翻转即可 AC代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tre ...
- VisualStudio使用HALCIN_NET控件
遵循以下步骤在一个应用中添加Halcon/.Net: 一. 定义工具箱 Halcon/.net 不仅提供了一个类库,而且提供了一个控件:HWindowControl,它包含一个显示图像处理结果的Hal ...
- spring boot 测试插件使用及result风格实例1&打包启动
本节主要内容: 1:spring boot 小插件使用 2:构建第一个简单的result风格的实例并访问 3:将项目打成jar包后启动并访问. 一:添加boot devtools插件: 执行完成后,查 ...
- spring boot原理分析启动依赖中parent帮我们干了什么
主要内容: 1:分析spring-boot-starter-parent 这个依赖 通过前面几篇文章的学习,我们感受到了spring boot的魅力.最明显的感觉就是pom.xml文件.代码少了很多. ...
- 基于phathomjs token 不定时无响应问题排查
问题描述 基于phathomjs的token池项目,基本原理是,打开淘宝页,获取匿名cookie,再由cookie中提取有效token,调用方通过该token获取淘宝数据 为方便基他人应用,封装了部分 ...
- django+nginx+gunicorn+supervisro部署
一.nginx 1.yum install -y nginx #默认安装后的配置文件路径:/etc/nginx/nginx.conf 2.新建项目的配置文件,因为默认配置文件会包含子配置文件,目录为 ...