过十点。

<?php
/*
The composite pattern is about treating the hierarchy of objects as a single object,
through a common interface. Where the objects are composed into three structures
and the client is oblivious to changes in the underlying structure because it only
consumes the common interface.
*/

interface Graphic {
    public function draw();
}

class CompositeGraphic implements Graphic {
    private $graphics = array();

    public function add($graphic) {
        $objId = spl_object_hash($graphic);
        $this->graphics[$objId] = $graphic;
    }

    public function remove($graphic) {
        $objId = spl_object_hash($graphic);
        unset($this->graphics[$objId]);
    }

    public function draw() {
        foreach ($this->graphics as $graphic) {
            $graphic->draw();
        }
    }
}

class Circle implements Graphic {
    public function draw() {
        echo 'draw-Circle<br/>';
    }
}

class Square implements Graphic {
    public function draw() {
        echo 'draw-Square<br/>';
    }
}

class Triangle implements Graphic {
    public function draw() {
        echo 'draw-Triangle<br/>';
    }
}

$circle = new Circle();
$square = new Square();
$triangle = new Triangle();

$compositeObj1 = new CompositeGraphic();
$compositeObj1->add($circle);
$compositeObj1->add($triangle);
$compositeObj1->draw();

$compositeObj2 = new CompositeGraphic();
$compositeObj2->add($circle);
$compositeObj2->add($square);
$compositeObj2->add($triangle);
$compositeObj2->remove($circle);
$compositeObj2->draw();
?>

php组合设计模式(composite pattern)的更多相关文章

  1. 使用C# (.NET Core) 实现组合设计模式 (Composite Pattern)

    本文的概念性内容来自深入浅出设计模式一书. 本文需结合上一篇文章(使用C# (.NET Core) 实现迭代器设计模式)一起看. 上一篇文章我们研究了多个菜单一起使用的问题. 需求变更 就当我们感觉我 ...

  2. 乐在其中设计模式(C#) - 组合模式(Composite Pattern)

    原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...

  3. 设计模式系列之组合模式(Composite Pattern)——树形结构的处理

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  4. 浅谈设计模式--组合模式(Composite Pattern)

    组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...

  5. 二十四种设计模式:组合模式(Composite Pattern)

    组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...

  6. 【设计模式】组合模式 Composite Pattern

    树形结构是软件行业很常见的一种结构,几乎随处可见,  比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...

  7. 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释

    组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...

  8. python 设计模式之组合模式Composite Pattern

    #引入一 文件夹对我们来说很熟悉,文件夹里面可以包含文件夹,也可以包含文件. 那么文件夹是个容器,文件夹里面的文件夹也是个容器,文件夹里面的文件是对象. 这是一个树形结构 咱们生活工作中常用的一种结构 ...

  9. 设计模式-12组合模式(Composite Pattern)

    1.模式动机 很多时候会存在"部分-整体"的关系,例如:大学中的部门与学院.总公司中的部门与分公司.学习用品中的书与书包.在软件开发中也是这样,例如,文件系统中的文件与文件夹.窗体 ...

随机推荐

  1. CDN惹的祸:记一次使用OSS设置跨域资源共享(CORS)不生效的问题

    原文: https://www.lastupdate.net/4669.html 昨天H5组的开发反馈了一个问题,说浏览器收不到跨域的配置,提示:Failed to load https://nnmj ...

  2. 理解UnrealBuildTool

    转自:https://zhuanlan.zhihu.com/p/57186557 介绍 虚幻引擎是当前比较流行的游戏开发引擎之一,许多流行的游戏都是虚幻引擎开发的. 然而“引擎”这个词在行业中的定义比 ...

  3. sql server数据表大小初始化

    sql server表在存储大数据和处理大数据表时,经常会遇到表空间越来越大,有时候会超出应该占有空间大小很多,此时如果表数据是压缩存储的,那么重新执行一下压缩脚本,数据的大小会重新初始化,然后再使用 ...

  4. Tomcat通过脚本自动部署

    1:autodeploy_tomcat_app.sh now=`date +%Y%m%d%H%M%S` tomcatPath=/home/test/apache-tomcat- backupPath= ...

  5. OpenCV4.1.2 QRCode解码体验测评(附源码+支持中文)

    目前官方Release的OpenCV最新版本为4.1.2,偶然看到更新信息里面QRCode解码性能有提升,所以迫不及待想尝试一下,因为上次测试了4.0版本的效果不太好. 下载和配置OpenCV的步骤此 ...

  6. 【mysql】搜索带\字符

    模糊查询 LIKE '%\\\%'

  7. ZYNQ笔记(6):普通自定义IP封装实现PL精准定时中断

    软件的定时中断很难控制精准触发沿的位置,可以通过 PL-PS 的中断完成精准的定时中断.PL 的中断通过 Verilog 代码产生,这样紧密结合 PS-PL 的处理,发挥各自的优势. 一.PL 侧定时 ...

  8. 更新Linux内核

    说明:为了安装Docker,当前虚拟机不满足要求,版本如下: [root@localhost116 ~]# uname -r -.el6.x86_64 [root@localhost116 ~]# c ...

  9. Replication:Distribution Reader

    在transactional replication中,在publication中执行了一个更新,例如:update table set col=? Where ?,如果table中含有大量的数据行, ...

  10. 【LeetCode】387. First Unique Character in a String

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...