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

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. 在css中设置图片的背景图,怎么设置图片纵向拉伸

    css中设置背景图拉伸填充,在css2.1之前这个背景的长宽值是不能被修改的. 实际的结果是只能重复显示,可以使用repeat,repeat-x,repeat-y,no-repeat这些属性来控制背景 ...

  2. JavaScript变量、数据类型、函数

    #转载请留言联系 说在前面: JavaScript 是一种弱类型语言,javascript的变量类型由它的值来决定. JavaScript语句的末尾用 ; 结束.空格没有特殊意义. 1.JavaScr ...

  3. 计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)

    A. An Olympian Math Problem 54.28% 1000ms 65536K   Alice, a student of grade 66, is thinking about a ...

  4. Codeforces 810 B. Summer sell-off

    B. Summer sell-off   time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. sqlmap的小试牛刀

    这次算是一次用sqlmap的例子,写的很水. 目的:通过工具扫描到了后台的数据库的地址(如下图),想通过sqlmap找到数据库的用户和密码进入到数据库(首先可以尝试一下root:root一般都是这个情 ...

  6. ubantu启动盘制作

    转载自http://jingyan.baidu.com/article/b24f6c82cf50e086bfe5dae9.html 1 首先打开UltraISO软件,没有的请百度搜索,下载安装,尽量下 ...

  7. 51nod 1459 迷宫游戏【最短路拓展】

    1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB   你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数.还有若干双向道路连 ...

  8. 2017中国大学生程序设计竞赛 - 女生专场A【模拟】

    A HDU - 6023 [题意]:求AC题数和总时长. [分析]:模拟.设置标记数组记录AC与否,再设置错题数组记录错的次数.罚时罚在该题上,该题没AC则不计入总时间,AC则计入.已经AC的题不用再 ...

  9. 代码编辑器[0] -> Vim/gVim[3] -> 像编程一样使用Vim

    像编程一样使用Vim 目录 为什么是Vim / Why Vim 从hjkl开始上路 -- 使用基本按键进行移动和编辑 / Start from <hjkl> 一次超速和翻车的体验 -- 使 ...

  10. Android学习--广播机制

    广播机制简介 Android的广播可以分为两种类型的,标准广播和有序的广播: 标准广播:  是一种完全异步执行的广播,在广播发出去之后,所有的广播接收器几乎是同一时接收到这条广播. 有序广播:  是一 ...