将对象组合成树形结构以表示整体-部分的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性.

UML:

示例代码:
透明组合:叶节点和子节点具有相同的接口

abstract class Component
{
protected $name; public function __construct($name)
{
$this->name = $name;
} abstract public function add(Component $node);
abstract public function remove(Component $node);
abstract public function display($deep);
} // 页节点
class Leaf extends Component
{
public function add(Component $node)
{
// 叶不能添加节点
} public function remove(Component $node)
{
// 叶不能删除节点
} public function display($deep)
{
echo str_repeat('-', $deep) . $this->name . PHP_EOL;
} } // 枝节点
class Composite extends Component
{
protected $nodes = array(); public function add(Component $node)
{
$this->nodes[] = $node;
} public function remove(Component $node)
{
unset($this->nodes[array_search($node, $this->nodes)]);
} public function display($deep)
{
echo str_repeat('-', $deep) . $this->name . PHP_EOL; foreach ($this->nodes as $node)
{
$node->display($deep + 2);
}
} } $root = new Composite('/root');
$root->add(new Leaf('/a.txt'));
$root->add(new Leaf('/b.txt')); $etc = new Composite('/etc');
$etc->add(new Leaf('httpd'));
$etc->add(new Leaf('nginx')); $root->add($etc);
$root->display(2);

  

示例代码:
安全组合:接口中不强制实现增加和删除节点,叶节点不具备该两项功能.

abstract class Component
{
protected $name; public function __construct($name)
{
$this->name = $name;
} abstract public function display($deep);
} // 页节点
class Leaf extends Component
{
public function display($deep)
{
echo str_repeat('-', $deep) . $this->name . PHP_EOL;
} } // 枝节点
class Composite extends Component
{
protected $nodes = array(); public function add(Component $node)
{
$this->nodes[] = $node;
} public function remove(Component $node)
{
unset($this->nodes[array_search($node, $this->nodes)]);
} public function display($deep)
{
echo str_repeat('-', $deep) . $this->name . PHP_EOL; foreach ($this->nodes as $node)
{
$node->display($deep + 2);
}
} } $root = new Composite('/root');
$root->add(new Leaf('/a.txt'));
$root->add(new Leaf('/b.txt')); $etc = new Composite('/etc');
$etc->add(new Leaf('httpd'));
$etc->add(new Leaf('nginx')); $root->add($etc);
$root->display(2);

  

S6:组合模式 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. 设计模式--组合模式Composite(结构型)

    一.概念 组合模式允许你将对象组合成树形结构来表现"整体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 二.UML图 1.Component(对象接口),定义 ...

随机推荐

  1. Spring MVC 基础篇4

    Spring MVC Controller中返回数据到页面 1.使用ModelAndView 进行数据返回到请求页面 2.利用Map类型的入参进行Controller返回到页面上 3.将数据放到Ses ...

  2. JAVA 批量执行测试用例

    如果多个测试用例在不同的类中,又需要一次性执行完所有的测试用例,则可以使用到Junit中的批量执行测试方法. 方法一 这种方式非常简单,不需要额外多写一行代码,Eclipse 本来就支持以项目或包为单 ...

  3. Qt笔记——绘图(QBitmap,QPixmap,QImage,QPicture)

    QPainter绘图 重写绘图事件,虚函数 如果窗口绘图,必须放在绘图事件里实现 绘图事件内部自动调用,窗口需要重绘的时候,状态改变 绘图设备(QPixmap,QImage,QBitmap,QPict ...

  4. configure.ac中AC_CHECK_LIB的问题

    编译Linux程序时,使用configure.ac生成的configure程序,时常会出现AC_CHECK_LIB检查某个库失败 而相应库通常是存在的,只是依赖于其他的库,此时,需要乃至AC_CHEC ...

  5. 神器mimikatz使用命令方法总结

    神器mimikatz使用命令方法总结 文章地址:http://www.isharepc.com/300.html mimikatz是一款功能强大的轻量级调试神器,通过它你可以提升进程权限注入进程读取进 ...

  6. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

  7. hdu6058

    hdu6058 题意 定义 \(f(l, r, k)\) 函数为区间 \([l, r]\) 第 \(k\) 大的数,如果 \(r - l + 1 < k\),\(f = 0\) .求 \(\su ...

  8. 【树状数组】Gym - 101147J - Whistle's New Car

    题意就是对每个点i,统计在其子树内(不含自身),且depj-depi<=xj的点有多少个. 把点分别按照dep-x和dep进行排序,离线处理, 每次把dep-x小于等于当前dep值的点插入树状数 ...

  9. 【博弈论】【SG函数】【找规律】Gym - 101147A - The game of Osho

    以后这种题还是不能空想,必须打个表看看,规律还是比较好找的……具体是啥看代码.用SG函数暴力的部分就不放了. #include<cstdio> using namespace std; i ...

  10. 【最大流】【费用流】bzoj1834 [ZJOI2010]network 网络扩容

    引用题解: 最大流+费用流. 第一问最大流即可. 第二问为“最小费用最大流”. 由题意,这一问的可转化为在上一问的“残量网络”上,扩大一些边的容量,使能从新的图中的最大流为k. 那么易得:对于还有剩余 ...