PHP组合模式Composite Pattern优点与实现过程
作者:php_gl12345678 发布时间:2023-05-29 02:10:44
标签:PHP,组合模式,Composite,Pattern
组合模式Composite Pattern是什么
组合模式是一种结构型模式,它允许你将对象组合成树形结构来表示“部分-整体”的层次关系。组合能让客户端以一致的方式处理个别对象和对象组合。
组合模式的优点
组合模式可以使客户端以一致的方式处理个别对象和对象组合,从而简化了客户端代码;
组合模式可以让我们更容易地增加新的组件,从而提高了系统的灵活性和可扩展性;
组合模式可以让我们更容易地管理复杂的对象结构,从而降低了系统的维护成本。
组合模式的实现
在 PHP 中,我们可以使用以下方式来实现组合模式:
<?php
// 抽象组件
abstract class Component
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
abstract public function add(Component $component);
abstract public function remove(Component $component);
abstract public function display($depth);
}
// 叶子组件
class Leaf extends Component
{
public function add(Component $component)
{
echo "Cannot add to a leaf.";
}
public function remove(Component $component)
{
echo "Cannot remove from a leaf.";
}
public function display($depth)
{
echo str_repeat("-", $depth) . $this->name . "\n";
}
}
// 容器组件
class Composite extends Component
{
private $children = array();
public function add(Component $component)
{
array_push($this->children, $component);
}
public function remove(Component $component)
{
$key = array_search($component, $this->children, true);
if ($key !== false) {
unset($this->children[$key]);
}
}
public function display($depth)
{
echo str_repeat("-", $depth) . $this->name . "\n";
foreach ($this->children as $component) {
$component->display($depth + 2);
}
}
}
// 客户端代码
$root = new Composite("root");
$root->add(new Leaf("Leaf A"));
$root->add(new Leaf("Leaf B"));
$comp = new Composite("Composite X");
$comp->add(new Leaf("Leaf XA"));
$comp->add(new Leaf("Leaf XB"));
$root->add($comp);
$root->add(new Leaf("Leaf C"));
$leaf = new Leaf("Leaf D");
$root->add($leaf);
$root->remove($leaf);
$root->display(1);
在上面的实现中,我们首先定义了一个抽象组件,并定义了叶子组件和容器组件。接着,我们在容器组件中定义了一个数组用于存储子组件,并实现了向容器组件中添加和删除子组件的方法。最后,我们在客户端代码中实例化了一个根组件,并向其中添加了叶子组件、容器组件和叶子组件,并通过调用根组件的display
方法来展示整个组件树。
组合模式的使用
<?php
$root = new Composite("root");
$root->add(new Leaf("Leaf A"));
$root->add(new Leaf("Leaf B"));
$comp = new Composite("Composite X");
$comp->add(new Leaf("Leaf XA"));
$comp->add(new Leaf("Leaf XB"));
$root->add($comp);
$root->add(new Leaf("Leaf C"));
$leaf = new Leaf("Leaf D");
$root->add($leaf);
$root->remove($leaf);
$root->display(1);
在上面的使用中,我们实例化了一个根组件,并向其中添加了叶子组件、容器组件和叶子组件,并通过调用根组件的display
方法来展示整个组件树。
总结
组合模式是一种非常常见的结构型模式,它可以让我们将对象组合成树形结构来表示“部分-整体”的层次关系。在实际开发中,我们可以根据具体的需求,选择不同的组合方式来管理复杂的对象结构,从而提高系统的灵活性和可扩展性。
来源:https://blog.csdn.net/weixin_39934453/article/details/129718020
0
投稿
猜你喜欢
- 几个月来好像就现在暂时无需求,稍微轻松一下,然后在Q群中发现有人提问,怎么用CSS实现数学公式“四又二分之一”。对于这个公式个人
- 因为工作的原因,开发过一个拆分字符串的SQL函数,现在把它贴出来,与大家共勉学习。该函数如下: &
- 第一次做完主从库同步后正常,但工作过程中发现有一个库的数据库没有同步起来,在另外一个mysql(3307)中于是:1、在主库中创建一个临时库
- Rs.Open参数说明在ASP中经常用Rs.Open sql,conn,1,1这样的方式打开数据库,但仍有一部分同行不知道这是嘛意思,现整理
- 如何用ASP建立Index Server查询对象并为其参数赋值?我们有两个方法可以做到这一点。一个方法是利用Index Server que
- 2天内的现实new文字 <%if DateDiff("d",rs("date"),date()
- 关于采集-防采集的我想大家都很清楚。这个就不多说,采集最终还是从页面源代码入手,所以只要没有规律想采集就难!由于一天自己在策划某个网站的时候
- 第一次写ASP类,实现功能:分段统计程序执行时间,输出统计表等.程序代码:Class ccClsProcessTimeRecord
- 原来工作中曾经碰到过UL列表里一些异常的表现,加上昨天看到了http://bbs.blueidea.com/thread-2984871-1
- 昨天在W3C看到,6月10日发布了新的 HTML 5 草案(Working Draft)。粗略的读了一下它提供的 新版本说明文档 ,作了一点
- 使用access数据库时可能用到的数据转换:类型转换涵数:函数 返回类型 expression 参数范围CBool Boolean 任何有效
- CAPTCHA,全称为“Completely Automated Public Turing test to tell Computers
- 看了OReilly.JavaScript.The.Definitive.Guide.5th.Edition.Aug.2006里的cookie
- 用了on error resume next则在这句往后的代码就算出错也会继续执行具体有没有错可以用err.number来判断err.num
- 有使用过VS2005开发工具的朋友或者其他语句如js中都有Try catch 语句块,那么在mysql中是否能有SQLserver的@@er
- 一、何谓ASP缓存/为什么要缓存当你的web站点采用asp技术建立的初期,可能感觉到的是asp * 页技术带来的便利性,以及随意修改性、自如
- 当你提交一个查询的时候,MySQL会分析它,看是否可以做一些优化使处理该查询的速度更快。这一部分将介绍查询优化器是如何工作的。如果你想知道M
- Mr.Think是一个喜欢简洁的人,喜欢如诗一样的代码.不喜欢.NET的代码,就是因为经常看到同事用VS后生成出来的一段段冗长的代码.在我的
- 从百度百科中扣去的这个图片轮播代码,图片向左不间断滚动,有停顿:<!DOCTYPE html PUBLIC "-//W3C/
- 在页面中自定义了changejs函数后页面提示错误:Active Server Pages 错误 'ASP 0138' 嵌套