首页 > 作文

php设计模式有哪些几种(php设计模式及应用场景)

更新时间:2023-04-05 23:51:50 阅读: 评论:0

组合模式(composite pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。

意图

将对象组合成树形结构以表示”部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

解决问题

它在我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。

uml图

该模式包含的角色及其职责

抽象根节点(component)

定义系统各层次对象的共有方法和属性,可以预先定义一些默认行为和属性。

树枝节点(composite)

定义树枝节点的行为,存储子节点,组合树枝节点和叶子节点形成一个树形结构。

叶子节点(leaf)

叶子节点对象,其下再无分支,是系统层次遍历的最小单位。

在本例子中:

优缺点

优点

复杂的对象分层次表示,添加新的节点容易。客户端调用简单,客户端可以一致的使用组合结构或其中单个对象。更容易在组合体内加入对象构件,客户端不必因为加入了新的对象构件而更改原有代码。

缺点

在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。使设计变得更加抽象,对象的业务规则如果很复杂,则实现组合模式具有很大挑战性,而且不是所有的方法都与叶子对象子类都有关联。

应用场景

当想表达对象的部分-整体的层次结构时,如公司的上下级关系,分销系统,产品品牌分类。希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象时。

案例背景

举一个例子:一家公司的组成通常都是多个部门组成的,现在我们有一家公司有多个部门,部门里面还有很多职位,我们用这个组合模式来看看。

组织架构

使用代码把上图转换成下图的形式表达

应用场景

component.php 抽象的公司架构(component)

abstract class component{    protected $name;    /**     * compo流产的危害nent constructor.     * @param $name     */    public function __construct($name){        $this->name = $name;    }    public abstract function operation($depth);    public 兰州大学是几本abstract function add(component $component);    pu网恋的危害blic abstract function remove(component $component);}

composite.php 具体的公司部门(composite)

class composite extends component{    private $componentlist;    public function operation($depth){        echo str_repeat('-', $depth) . $this->name . php_eol;        foreach ($this->compone山西省行政区划ntlist as $component) {            $component->operation($depth + 2);        }    }    public function add(component $component){        $this->componentlist[] = $component;    }    public function remove(component $component){        $position = 0;        foreach ($this->componentlist as $child) {            ++$position;            if ($child == $component) {                array_slice($this->componentlist, $position, 1);            }        }    }    public function getchild(int $i){        return $this->componentlist[$i];    }}

leaf.php 公司部门最终的职位(leaf)

class leaf extends component{    public function operation($depth){        echo str_repeat('-', $depth) . $this->name . php_eol;    }    public function add(component $component){        echo "cannot add to a leaf".php_eol;    }    public function remove(component $component){        echo "cannot remove from a leaf".php_eol;    }}

调用代码:

$root = new composite("公司");// 添加人事部门$rs = new composite("人事部门");// 添加人事部门下面的职位$rs->add(new leaf("人事总监"));$rs->add(new leaf("人事主管"));$rs->adqq摄像头不能用d(new leaf("人事专员"));$root->add($rs);// 添加财务部门$cw = new composite("财务部门");// 添加部门下面的职位$cw->add(new leaf("财务总监"));$cw->add(new leaf("会计"));$cw->add(new leaf("出纳"));$root->add($cw);$root->operation(0);$child = $root->getchild(0);print_r($child);

输出结果:

公司--人事部门----人事总监----人事主管----人事专员--财务部门----财务总监----会计----出纳// 通过 getchild 获取的某个部门对象composite object(    [composite:private] => array        (            [0] => leaf object                (                    [name:protected] => 人事总监                )            [1] => leaf object                (                    [name:protected] => 人事主管                )            [2] => leaf object                (                    [name:protected] => 人事专员                )        )    [name:protected] => 人事部门)

如果觉得文章还不错,请把文章分享给更多的人学习,在文章中发现有误的地方也希望各位指出更正。现有误的地方也希望各位指出更正。

本文发布于:2023-04-05 23:51:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/d5644d9e3f61854bd2f8d5f69d0efad3.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:php设计模式有哪些几种(php设计模式及应用场景).doc

本文 PDF 下载地址:php设计模式有哪些几种(php设计模式及应用场景).pdf

上一篇:梦见好多大蛇
下一篇:返回列表
标签:组合   对象   节点   模式
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图