PHP设计模式系列(十四):组合模式

本文介绍了PHP设计模式中的组合模式,旨在阐述如何用组合模式构建对象的树形结构,以体现部分-整体的层次关系,并确保对单个对象和组合对象的操作一致性。文章详细讲解了模式结构,包括Component、Leaf和Composite的角色,并提供了PHP代码实现及运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

组合模式

组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。掌握组合模式的重点是要理解清楚 “部分/整体” 还有 ”单个对象“ 与 “组合对象” 的含义。

模式结构

  • Component :组合中的对象声明接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理Component子部件。
  • Leaf:叶子对象。叶子结点没有子结点。
  • Composite:容器对象,定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关操作,如增加(add)和删除(remove)等。

结构图

这里写图片描述

PHP代码实现

<?php
/**
 * 组合模式
 */
//组合中的对象声明接口
abstract class Component
{
    public function __construct($name)
    {
        $this->name=$name;
    }
    abstract public function Add(Component $c);
    abstract public function Remove(Component $c);
    abstract public function Display($depth);
}

//叶子对象
class Leaf extends Component
{
    public function Add(Component $c){
        var_dump('add');
    }
    public function Remove(Component $c){
        var_dump('Remove');
    }
    public function Display($depth){
        var_dump(str_repeat('-',$depth).$this->name);
    }
}

//容器对象
class Composite extends Component
{
    private $children=[];
    public function Add(Component $c){
        $this->children[]=$c;
    }
    public function Remove(Component $c){
        foreach ($this->children as $child){
            if ($child!= $c){
                $a[]=$child;
            }
        }
        $this->children=$a;
    }
    public function Display($depth){
        var_dump(str_repeat('-',$depth).$this->name);
        foreach ($this->children as $value){
            $value->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);

$comp2=new Composite('Composite XY');
$comp2->Add(new Leaf('Leaf XYA'));
$comp2->Add(new Leaf('Leaf XYB'));

$comp->Add($comp2);

$root->Add(new Leaf('Leaf C'));
$leaf=new Leaf('Leaf D');
$root->Add($leaf);
$root->Display(1);

运行结果

string '-root' (length=5)
string '---Leaf A' (length=9)
string '---Leaf B' (length=9)
string '---Composite X' (length=14)
string '-----Leaf XA' (length=12)
string '-----Leaf XB' (length=12)
string '-----Composite XY' (length=17)
string '-------Leaf XYA' (length=15)
string '-------Leaf XYB' (length=15)
string '---Leaf C' (length=9)
string '---Leaf D' (length=9)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值