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

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. 使用echarts展示线状图信息的时候数据部分数据因为x轴的数据显示不全而隐藏的问题

    在使用echarts来展示数据时,因为数据很多的原因导致x轴显示不全,然后有些数据也隐藏在图表中,所以这个时候我们要在 series 中设置一个属性,让所有的数据都能够展示出来,这里我们需要添加的属性 ...

  2. django添加导包路径

    在设置文件里: import sys sys.path.insert(0,os.path.join(BASE_DIR,"要导包的目录名")) 用pycharm时,如果导包后没有自动 ...

  3. redis使用中的常见错误

    1.2016年12月17日  启动redis报错,错误信息如下: 解决办法:redis没有正常关闭(redis安装在虚拟机上,直接杀死了虚拟机进程) 导致redis.pid文件一直被锁定,重启redi ...

  4. pythn抓取网页小例子

    import urllib.request import re from tkinter import * win = Tk() win.geometry('500x300+400+300') t = ...

  5. java 获取当前系统时间

    Java的Date获取时间函数都是deprecated 可以使用: https://stackoverflow.com/questions/5175728/how-to-get-the-current ...

  6. dokcer常用命令

    文章来自于 CSDN docker常用命令详解 docker常用命令分类 常用命令 docker run -d --name express-docker-demo --restart=always ...

  7. Unity中Instantiate物体失效问题

    才开始学Unity,开始总是这样用Instantiate函数: GameObject temp = (GameObject)Instantiate(bulletSource, transform.po ...

  8. CodeFirst模式开发涉及到mysql简单使用

    在和同学写自己的小项目中,这次大胆的用到了“mysql”,毕竟是第一次在项目中使用,可我和同学就犯难了,我们没有真正意义上学过mysql,或者可以说,使用过mysql,当我们把项目放在www.git. ...

  9. lca最短公共祖先模板(hdu2586)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 #include<iostream> #include<cstdio> ...

  10. 简化的INSERT语句

    INSERT语句中也并不需要我们指定表中的所有列,比如在插入数据的时候某些字段没有值,我们可以忽略这些字段.下面我们插入一条没有备注信息的数据: INSERT INTO T_Person(FAge,F ...