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

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. OleDbDataAdapter具体使用11

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. 项目问题整理(it)

    1,很(屎)优(一)雅(样)的IE9兼容问题: --webuploader在webkit浏览器中自动使用h5上传,但在IE中需要配置支持flash,特别注意两点: ①Upload.swf路径问题不正确 ...

  3. 【C语言】++(a++)的写法是错的

    http://bbs.csdn.net/topics/390764053 a++得到的是一个右值,++操作需要的是一个左值. ------------------------------------- ...

  4. Delphi2007新功能 -- 有限的栈对象

    今天使用Delphi2007,一个误输入,无意中发现Delphi2007的record类型居然能够和TObject一样定义方法和属性,而且不需要调用类似TObject.Create方法就能生成一个re ...

  5. Selenium2+python自动化27-查看selenium API【转载】

    前言 前面都是点点滴滴的介绍selenium的一些api使用方法,那么selenium的api到底有多少呢?本篇就叫大家如何去查看selenium api,不求人,无需伸手找人要,在自己电脑就有. p ...

  6. rertful规范

    RESTful API的理解 断言 assert 条件(True)执行下面代码 assert 条件(False) 报错 什么是接口? 1- URL,用于进行系统之间操作数据. 2- 面向对象接口,用于 ...

  7. opencv mat

    mat基础教程: http://blog.csdn.net/sinat_31802439/article/details/50083291 mat 初始化: Mat M(,,CV_32FC1); Ma ...

  8. bzoj3942

    有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程.将s中的每一个字符压入栈暴力将s中的字符和 ...

  9. ASP.NET Core 2.2 基础知识(四) URL重写中间件

    说到URL重写就不得不提URL重定向. URL重定向 URL重定向是客户端操作,指示客户端访问另一个地址的资源.这需要往返服务器,并且当客户端对资源发出请求时,返回客户端的重定向URL会出现在浏览器的 ...

  10. 【kd-tree】CDOJ - 1170 - 红与蓝

    kd-tree模板题,对红点建立kd-tree,用每个蓝点查询,更新最小值即可. #include<cstdio> #include<cmath> #include<al ...