S6:组合模式 Composite
将对象组合成树形结构以表示整体-部分的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性.
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的更多相关文章
- 组合模式/composite模式/对象结构型模式
组合模式/composite模式/对象结构型 意图 将对象组合成树形结构以表示"整体--部分"的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 动机 C ...
- 浅谈设计模式--组合模式(Composite Pattern)
组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...
- 二十四种设计模式:组合模式(Composite Pattern)
组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...
- 设计模式(七)组合模式Composite(结构型)
设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 ...
- 乐在其中设计模式(C#) - 组合模式(Composite Pattern)
原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...
- 【设计模式】组合模式 Composite Pattern
树形结构是软件行业很常见的一种结构,几乎随处可见, 比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...
- 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释
组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...
- 设计模式系列之组合模式(Composite Pattern)——树形结构的处理
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 设计模式--组合模式Composite(结构型)
一.概念 组合模式允许你将对象组合成树形结构来表现"整体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 二.UML图 1.Component(对象接口),定义 ...
随机推荐
- Selenium2+python自动化72-logging日志使用【转载】
前言 脚本运行的时候,有时候不知道用例的执行情况,这时候可以加入日志,这样出现问题后方便查阅,也容易排查哪些用例执行了,哪些没有执行. 一.封装logging模块 1.关于logging日志的介绍,我 ...
- 【hdoj_1050】Moving Tables
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1050 题意为: 为了叙述方便,把一个房间里面的桌子移动到另一个房间称为一个移动,给出若干个要求完成的移 ...
- ACM-ICPC北京赛区(2017)网络赛2【后缀数组+Java//不会】
#1579 : Reverse Suffix Array 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There is a strong data structure ...
- JavaScript防止重复提交表单
往往有些用户网络慢或者其他问题,在提交表单的时候使劲点击保存提交按钮,在提交表单的时候加上下面的代码,即可以限制在一定时间内,只有一次点击是有效的. <script> var mypret ...
- 简单DP【p1934】封印
Description 很久以前,魔界大旱,水井全部干涸,温度也越来越高.为了拯救居民,夜叉族国王龙溟希望能打破神魔之井,进入人界"窃取"水灵珠,以修复大地水脉.可是六界之间皆有封 ...
- linux历史命令
"忘记历史的Linux用户注定要输入很多信息.” 这也让强有力的历史命令(包括Bash shell的历史变体)不仅在援引之前执行命令而不需重新输入它们时有用,在调用其它很少用到的命令时也有用 ...
- centos7下ip转发的配置
1.先确认ipv4配置了转发设置 vim /etc/sysctl.conf #命令1(编辑配置文件) net.ipv4.ip_forward=1 ...
- 理解XML-RPC
有关XML-RPC http://baike.baidu.com/link?url=ejidFtjelUzPv75VBm5_XrzSbHtFgArYY47S1s1NK2_m-auOr10sTeRh6U ...
- Github上的iOS资料-个人记录
动画 awesome-ios-animation收集了iOS平台下比较主流炫酷的几款动画框架 RCTRefreshControlqq的下拉刷新 TBIconTransitionKiticon 的点击动 ...
- 使用UNetbootin工具制作的CentOS 6.9镜像U盘在启动安装过程中出现:unable to read package metadata.this may be due to a missing repodata directory
1.制作: 2.重命名文件 (前) (后) 这些文件是拷贝另一个得来的,并且后面的命名是根据repomd.xm这个文件来的. 参考: http://blog.csdn.net/maijunjin/ar ...