php组合设计模式(composite pattern)
生活随笔
收集整理的這篇文章主要介紹了
php组合设计模式(composite pattern)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
過十點(diǎn)。
<?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(); ?>轉(zhuǎn)載于:https://www.cnblogs.com/aguncn/p/11182294.html
總結(jié)
以上是生活随笔為你收集整理的php组合设计模式(composite pattern)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国内开源项目无法形成气候且难以持续性的问
- 下一篇: Mysq表的创建和l数据类型