加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP设计模式—组合模式

发布时间:2020-12-13 21:24:42 所属栏目:PHP教程 来源:网络整理
导读:? 定义: 组合模式(Composite): 将对象组合成树形结构以表示“ 部分-整体 ”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。 当你发现需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一的

?

定义:

组合模式(Composite):将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。当你发现需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一的使用组合结构中的所有对象时,就应该考虑用组合模式了。

?

实现方式:

1、透明方式:叶节点和枝节点对于外界没有什么区别,它们具备完全一致的行为接口,问题则是叶节点会有冗余方法。

2、安全方式:叶节点中的冗余代码不实现,问题则是由于不够透明,所以叶节点和枝节点将不具有相同的接口,客户端的调用需要做相应的判断,带来了不便。

?

代码实例:

一、透明方式实现:

// 1、抽象类Component.php
/**
 * 包含叶节点和枝节点方法的抽象类
 * Class Component
 */
abstract class Component
{
    /**
     * @var
     */
    protected $name;

    *
     * Component constructor.
     * @param $name
     public function __construct()
    {
        $this->name = ;
    }


    *
     * 添加叶节点或枝节点
     * @param Component $component
     * @return mixed
     function add(Component $component);


    *
     * @param $depth
     * @return mixed
     function display($depth);
}


 2、枝节点Composite.php*
 * 枝节点
 * Class Composite
 class Composite extends*
     * @var array
     $children = [];

    *
     * @param Component $component
     * @return mixed|void
     )
    {
         TODO: Implement add() method.
        $this->children[] = ;
    }

     TODO: Implement display() method.
        $nameStr = str_repeat('-',$depth) . $this->name . '<br>';
        foreach ($this->children as ) {
            $nameStr .= $component->display($depth + 2);
        }
        return $nameStr;
    }
}


 3、叶节点Leaf.php*
 * 叶节点
 * Class Leaf
 class Leaf *
     * 叶节点不需要添加子节点,但为了保持叶节点和枝节点一致,代码冗余,透明方式
     * @param Component $component
     * @return mixed|string
      TODO: Implement add() method.
        return '叶节点不能添加子节点' . '<br>' TODO: Implement display() method.
        return ;
    }
}

调用:

 生成树根root,根上长出两叶LeafA和LeafB
$root = new Composite("root");
$root->add(new Leaf("Leaf A"));
new Leaf("Leaf B"));

 根上长出分支 CompositeX,分支上也有两叶LeafXA和LeafXB
$comp = new Composite("Composite X"$comp->add(new Leaf("Leaf XA"new Leaf("Leaf XB"$root->add($comp);

 在CompositeX分支上再长出分支CompositeXY,分支上也有两叶LeafXYA和LeafXYB
$comp2 = new Composite("Composite XY"$comp2->add(new Leaf("Leaf XYA"new Leaf("Leaf XYB"$comp->add($comp2echo $root->display(2);

结果:

--root
----Leaf A
----Leaf B
----Composite X
------Leaf XA
------Leaf XB
------Composite XY
--------Leaf XYA
--------Leaf XYB

?

二、安全方式实现:

Leaf XYA
--------Leaf XYB

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读