设计模式详解 PHP版
  • 设计模式简介&原则
  • 常见设计模式
    • 工厂模式(Factory)
      • 简单工厂模式(Simple Factory)
      • 工厂方法模式(Factory Method)
      • 抽象工厂模式(Abstract Factory)
      • 三种工厂模式对比
    • 单例模式(Singleton)
      • 场景*memcache操作类
    • 注册树模式
    • 适配器模式(Adapter)
    • 策略模式(Strategy)
    • 数据对象映射模式
    • 观察者模式(observer)
    • 原型模式(Prototype)
    • 装饰器模式(Decorator)
    • 迭代器模式 (Iteration)
    • 桥接模式(Bridge)
    • 建造者模式(Builder)
    • 命令行模式(Command)
    • 组合模式(Composite)
    • 外观模式(Facade)
    • 享元模式(Flyweight)
    • 解释器模式(Interpreter)
    • 中介者模式(Mediator)
    • 备忘录模式(Memento)
    • 代理模式(Proxy)
    • 责任链模式(Responsibility Chain)
    • 状态模式(State)
    • 模板方法模式(Template Method)
    • 访问者模式(Visitor)
  • 资料
Powered by GitBook
On this page

Was this helpful?

  1. 常见设计模式

组合模式(Composite)

模式架构简单代码实现示例:

<?php
//安全式合成模式
interface Component {
    public function getComposite(); //返回自己的实例
    public function operation();
}

class Composite implements Component { // 树枝组件角色
    private $_composites;
    public function __construct() { $this->_composites = array(); }
    public function getComposite() { return $this; }
     public function operation() {
         foreach ($this->_composites as $composite) {
            $composite->operation();
        }
     }

    public function add(Component $component) {  //聚集管理方法 添加一个子对象
        $this->_composites[] = $component;
    }

    public function remove(Component $component) { // 聚集管理方法 删除一个子对象
        foreach ($this->_composites as $key => $row) {
            if ($component == $row) { unset($this->_composites[$key]); return TRUE; }
        } 
        return FALSE;
    }
    public function getChild() { // 聚集管理方法 返回所有的子对象
       return $this->_composites;
    }

}

class Leaf implements Component {
    private $_name; 
    public function __construct($name) { $this->_name = $name; }
    public function operation() {}
    public function getComposite() {return null;}
}

// client
$leaf1 = new Leaf('first');
$leaf2 = new Leaf('second');
$composite = new Composite();
$composite->add($leaf1);
$composite->add($leaf2);
$composite->operation();
$composite->remove($leaf2);
$composite->operation();
//透明式合成模式
interface Component { // 抽象组件角色
    public function getComposite(); // 返回自己的实例
    public function operation(); // 示例方法
    public function add(Component $component); // 聚集管理方法,添加一个子对象
    public function remove(Component $component); // 聚集管理方法 删除一个子对象
    public function getChild(); // 聚集管理方法 返回所有的子对象
}

class Composite implements Component { // 树枝组件角色
    private $_composites;
    public function __construct() { $this->_composites = array(); } 
    public function getComposite() { return $this; }
    public function operation() { // 示例方法,调用各个子对象的operation方法
        foreach ($this->_composites as $composite) {
            $composite->operation();
        }
    }
    public function add(Component $component) { // 聚集管理方法 添加一个子对象
        $this->_composites[] = $component;
    }
    public function remove(Component $component) { // 聚集管理方法 删除一个子对象
        foreach ($this->_composites as $key => $row) {
            if ($component == $row) { unset($this->_composites[$key]); return TRUE; }
        } 
        return FALSE;
    }
    public function getChild() { // 聚集管理方法 返回所有的子对象
       return $this->_composites;
    }

}

class Leaf implements Component {
    private $_name;
    public function __construct($name) {$this->_name = $name;}
    public function operation() {echo $this->_name."<br>";}
    public function getComposite() { return null; }
    public function add(Component $component) { return FALSE; }
    public function remove(Component $component) { return FALSE; }
    public function getChild() { return null; }
}

// client 
$leaf1 = new Leaf('first');
$leaf2 = new Leaf('second');
$composite = new Composite();
$composite->add($leaf1);
$composite->add($leaf2);
$composite->operation();
$composite->remove($leaf2);
$composite->operation();
Previous命令行模式(Command)Next外观模式(Facade)

Last updated 6 years ago

Was this helpful?