PHP设计模式 - 合成模式
组合模式(Composite Pattern)有时候又叫做部分-整体模式,用于将对象组合成树形结构以表示“部分-整体”的层次关系。组合模式使得用户对单个对象和组合对象的使用具有一致性。
常见使用场景:如树形菜单、文件夹菜单、部门组织架构图等。
<?php
/**
*
*安全式合成模式
*/
interface Component {
public function getComposite(); //返回自己的实例
public function operation();
} class Composite implements Component { // 树枝组件角色
private $_composites;
public function __construct() { $this->_composites = array(); }
public function getComposite() { return $this; }
public function operation() {
foreach ($this->_composites as $composite) {
$composite->operation();
}
} public function add(Component $component) { //聚集管理方法 添加一个子对象
$this->_composites[] = $component;
} public function remove(Component $component) { // 聚集管理方法 删除一个子对象
foreach ($this->_composites as $key => $row) {
if ($component == $row) { unset($this->_composites[$key]); return TRUE; }
}
return FALSE;
} public function getChild() { // 聚集管理方法 返回所有的子对象
return $this->_composites;
} } class Leaf implements Component {
private $_name;
public function __construct($name) { $this->_name = $name; }
public function operation() {}
public function getComposite() {return null;}
} // client
$leaf1 = new Leaf('first');
$leaf2 = new Leaf('second'); $composite = new Composite();
$composite->add($leaf1);
$composite->add($leaf2);
$composite->operation(); $composite->remove($leaf2);
$composite->operation(); /**
*
*透明式合成模式
*/
interface Component { // 抽象组件角色
public function getComposite(); // 返回自己的实例
public function operation(); // 示例方法
public function add(Component $component); // 聚集管理方法,添加一个子对象
public function remove(Component $component); // 聚集管理方法 删除一个子对象
public function getChild(); // 聚集管理方法 返回所有的子对象
} class Composite implements Component { // 树枝组件角色
private $_composites;
public function __construct() { $this->_composites = array(); }
public function getComposite() { return $this; }
public function operation() { // 示例方法,调用各个子对象的operation方法
foreach ($this->_composites as $composite) {
$composite->operation();
}
}
public function add(Component $component) { // 聚集管理方法 添加一个子对象
$this->_composites[] = $component;
}
public function remove(Component $component) { // 聚集管理方法 删除一个子对象
foreach ($this->_composites as $key => $row) {
if ($component == $row) { unset($this->_composites[$key]); return TRUE; }
}
return FALSE;
}
public function getChild() { // 聚集管理方法 返回所有的子对象
return $this->_composites;
} } class Leaf implements Component {
private $_name;
public function __construct($name) {$this->_name = $name;}
public function operation() {echo $this->_name."<br>";}
public function getComposite() { return null; }
public function add(Component $component) { return FALSE; }
public function remove(Component $component) { return FALSE; }
public function getChild() { return null; }
} // client
$leaf1 = new Leaf('first');
$leaf2 = new Leaf('second'); $composite = new Composite();
$composite->add($leaf1);
$composite->add($leaf2);
$composite->operation(); $composite->remove($leaf2);
$composite->operation(); ?>
PHP设计模式 - 合成模式的更多相关文章
- Java设计模式-合成模式
合成模式有时也叫组合模式,对象组合成树形结构以表示"部分-整体"的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性.掌握组合模式的重点是要理解清楚 "部分/ ...
- java设计模式---合成模式3
实例 下面以一个逻辑树为例子,以上面的原理图为蓝本,看看如何实现并如何使用这个树,这个结构很简单,但是如何去使用树,遍历树.为我所用还是有一定难度的. 这里主要用到树的递归遍历,如何递归.如何控制 ...
- java设计模式---合成模式2
合成模式属于对象的结构模式,有时又叫做"部分--整体"模式.合成模式将对象组织到树结构中,可以用来描述整体与部分的关系.合成模式可以使客户端将单纯元素与复合元素同等看待. 合成模式 ...
- 组合模式 合成模式 COMPOSITE 结构型 设计模式(十一)
组合模式(合成模式 COMPOSITE) 意图 将对象组合成树形结构以表示“部分-整体”的层次结构. Composite使得用户对单个对象和组合对象的使用具有一致性. 树形结构介绍 为了便于理解, ...
- JAVA设计模式之合成模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述合成(Composite)模式的: 合成模式属于对象的结构模式,有时又叫做“部分——整体”模式.合成模式将对象组织到树结构中,可以用来描述 ...
- 设计模式_Composite_合成模式
形象例子: Mary今天过生日.“我过生日,你要送我一件礼物.”“嗯,好吧,去 商店,你自己挑.”“这件T恤挺漂亮,买,这条裙子好看,买,这个包也不错,买.”“喂,买了三件了呀,我只答应送一件礼物的哦 ...
- Java设计模式(六)合成模式 享元模式
(十一)合成模式 Composite 合成模式是一组对象的组合,这些对象能够是容器对象,也能够是单对象.组对象同意包括单对象,也能够包括其它组对象,要为组合对象和单对象定义共同的行为.合成模式的意义是 ...
- 设计模式之合成模式(Java语言描述)
<JAVA与模式>一书中开头是这样描述合成(Composite)模式的: 合成模式属于对象的结构模式,有时又叫做"部分--整体"模式.合成模式将对象组织到树结构中,可以 ...
- linkin大话设计模式--常用模式总结
linkin大话设计模式--常用模式总结 一,常用设计模式定义 Abstract Factory(抽象工厂模式):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. Adapter( ...
随机推荐
- (生鲜项目)05. RESTful api, 和 VUE
第一步: 什么是 RESTful api 总结: 使用http协议作为介质, 达到客户端修改服务器端资源的目的, 服务器只需要提供指定的api接口, 客户端根据http协议中的post/get/put ...
- wiki with 35(dp+矩阵快速幂)
Problem J. Wiki with 35Input file: standard input Time limit: 1 secondOutput file: standard output M ...
- Bias, Variance and the Trade-off
偏差,方差以及两者权衡 偏差是由模型简化的假设,使目标函数更容易学习. 一般来说,参数化算法有很高的偏差,使它们学习起来更快,更容易理解,但通常不那么灵活.反过来,它们在复杂问题上的预测性能更低,无法 ...
- 20199302《Linux内核原理与分析》第十二周作业
ShellShock攻击实验 什么是ShellShock? Shellshock,又称Bashdoor,是在Unix中广泛使用的Bash shell中的一个安全漏洞,首次于2014年9月24日公开.许 ...
- gj的交换机在升级了ios之后最新数据不刷新,
下午2点开始升级5点结束,之后监控项获取不到最新数据,显示网络接口一直是down的状态,但是登上设备之后显示的是正常up状态, 怀疑是自动发现规则的问题,但是查看之后都是1个小时,应该不会, 这时候诡 ...
- 【字符串】后缀数组SA
后缀数组 概念 实际上就是将一个字符串的所有后缀按照字典序排序 得到了两个数组 \(sa[i]\) 和 \(rk[i]\),其中 \(sa[i]\) 表示排名为 i 的后缀,\(rk[i]\) 表示后 ...
- 洛谷P1560 蜗牛的旅行
题目 搜索,注意判断特殊情况,并且区分开什么时候转弯什么时候停止.然后转弯的时候更是要注意是否会进入障碍. #include <bits/stdc++.h> using namespace ...
- shell脚本编程基础知识点
整数比较: -eq:测试两个整数是否相等:相等为真,不等为假 -ne:测试两个整数是否不等:不等为真,相等为假 -gt:测试一个数是否大于另一个数:大于为真,否则为假 -lt:测试一个数是否小于另一个 ...
- win7安装composer Failed to decode zlib stream
今天学习php的时候想安装一下composer,刚开始采用的是exe文件的安装方式,可是安装了好几次都没有安装成功,如下图: 可能还有其他的一些错误,所以这里我就换了一个方式安装,就是自己手动来安装c ...
- CodeMirror在线代码编辑器使用
CodeMirror官网地址为:https://codemirror.net/ CodeMirror作为一款代码编辑器,其应用场景主要是在线网站写代码.如现在的leetcode.洛谷.code-vs等 ...