php组合设计模式(composite pattern)
过十点。
<?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)的更多相关文章
- 使用C# (.NET Core) 实现组合设计模式 (Composite Pattern)
		
本文的概念性内容来自深入浅出设计模式一书. 本文需结合上一篇文章(使用C# (.NET Core) 实现迭代器设计模式)一起看. 上一篇文章我们研究了多个菜单一起使用的问题. 需求变更 就当我们感觉我 ...
 - 乐在其中设计模式(C#) - 组合模式(Composite Pattern)
		
原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...
 - 设计模式系列之组合模式(Composite Pattern)——树形结构的处理
		
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
 - 浅谈设计模式--组合模式(Composite Pattern)
		
组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...
 - 二十四种设计模式:组合模式(Composite Pattern)
		
组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...
 - 【设计模式】组合模式 Composite Pattern
		
树形结构是软件行业很常见的一种结构,几乎随处可见, 比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...
 - 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释
		
组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...
 - python 设计模式之组合模式Composite Pattern
		
#引入一 文件夹对我们来说很熟悉,文件夹里面可以包含文件夹,也可以包含文件. 那么文件夹是个容器,文件夹里面的文件夹也是个容器,文件夹里面的文件是对象. 这是一个树形结构 咱们生活工作中常用的一种结构 ...
 - 设计模式-12组合模式(Composite Pattern)
		
1.模式动机 很多时候会存在"部分-整体"的关系,例如:大学中的部门与学院.总公司中的部门与分公司.学习用品中的书与书包.在软件开发中也是这样,例如,文件系统中的文件与文件夹.窗体 ...
 
随机推荐
- concurrent(五)同步辅助器CountDownLatch & 源码分析
			
参考文档: https://blog.csdn.net/zxdfc/article/details/52752803 简介 CountDownLatch是一个同步辅助类.允许一个或多个线程等待其他线程 ...
 - Leetcode 5274. 停在原地的方案数
			
纪念第一次正式参加,听说这次题目很水,感觉确实不是很难,一般前两题都很简单,这次,到第三题的时候,都还可以做,emm...... 实际代码记录: #include <iostream> # ...
 - 【RabbitMQ学习之二】RabbitMQ四种交换机模式应用
			
环境 win7 rabbitmq-server-3.7.17 Erlang 22.1 一.概念1.队列队列用于临时存储消息和转发消息.队列类型有两种,即时队列和延时队列. 即时队列:队列中的消息会被立 ...
 - Kafka Offset Monitor页面显示空白
			
下载包:https://github.com/Morningstar/kafka-offset-monitor.git 解决:jar包内\KafkaOffsetMonitor-assembly-0.2 ...
 - Atlassian JIRA 插件开发之一  环境搭建
			
参考 https://developer.atlassian.com/server/framework/atlassian-sdk/ download the SDK 说明 Download the ...
 - 【Netcore】使用 Magic生成器 ,零代码实现CRUD - HTTP REST 之接口
			
软件介绍: Magic是一个CRUD后端生成器,内置于ASP.NET内核中.它的目的是让你“神奇地”做一些无聊的事情,通过使用自动化技术,创建80%的CRUD端点,自动包装MySQL或MS SQL S ...
 - [转帖]kafka 如何保证数据不丢失
			
kafka 如何保证数据不丢失 https://www.cnblogs.com/MrRightZhao/p/11498952.html 一般我们在用到这种消息中件的时候,肯定会考虑要怎样才能保证数 ...
 - Springboot Actuator之八:actuator的执行原理
			
本文接着<Springboot Actuator之七:actuator 中原生endpoint源码解析1>,前面主要分析了原生endpoint的作用. 现在着重了解actuator的执行原 ...
 - 洛谷P5524:[Ynoi2012]D1T1——题解
			
https://www.luogu.org/problem/P5524 看着能做就当线段树复健题了. 根据高中知识我们有 $sin(a+b)=sin(a)cos(b)+cos(a)sin(b)$ $c ...
 - Scala 数组操作之Array、ArrayBuffer以及遍历数组
			
ArrayBuffer 在Scala中,如果需要类似于Java中的ArrayList这种长度可变的集合类,则可以使用ArrayBuffer. // 如果不想每次都使用全限定名,则可以预先导入Array ...