PHP常用设计模式

1、单例模式指在整个应用中只有一个对象实例的设计模式

class Single {
    public $rand;
    static private $instance; // 类直接调用
    final private function __construct() { // private防止外部new
        $this->rand = mt_rand(1, 100);
    }
    static public function getInstance() {
        if (!self::$instance) { // 判断$instance是否赋值
            self::$instance = new self;
        }
        return self::$instance; 
    }
}
$obj1 = Single::getInstance();
$obj2 = Single::getInstance();
var_dump($obj1 === $obj2); // true

 2、工厂模式指通过工厂类调用自身静态方法来生产对象实例的设计模式

//接单例模式代码
class FactoryClass {
    static public function factory() {
        return Single::getInstance();
    }
}
var_dump(FactoryClass::factory());

3、注册器模式指将对象注册到全局树上,可直接设置获取注销对象

class Register {
    static protected $objects; //全局树array
    static public function set($alias, $object) {
        self::$objects[$alias] = $object; //设置
    }
    static public function get($alias) {
        return self::$objects[$alias]; //获取
    }
    static public function _unset($alias) {
        unset(self::$objects[$alias]); //注销
    }
}
Register::set('rand', FactoryClass::factory()); //设置别名为rand的对象
var_dump(Register::get('rand')); //获取别名为rand的对象
Register::_unset('rand');   //销毁别名为rand的对象
var_dump(Register::get('rand')); //Notice: Undefined index: rand 

 4、适配器模式指将一个类的接口转换成客户期望的另一个类的接口,让原本接口不兼容的类合作无间。

适配器分为类适配器和对象适配器,类适配器采用“多继承”的实现方式,导致不良的高耦合,对象适配器采用“对象组合”的方式,更符合松耦合精神。

  4.1 类适配器

//类适配器使用的是继承
/**
 * 目标
 */
interface Target {
    public function method1(); //源类也有的方法1
    public function method2(); //源类没有的方法2
}

/**
 * 源
 */
class Source {
    public function method1() {
        echo '我是源类含有的方法一 <br>';
    }
}

/**
 * 类适配器
 */
class Adapter extends Source implements Target {
    public function method2() {
        echo '我是适配器补充的方法二 <br>';
    }
}

/**
 * 客户端
 */
class Client {
    static public function main() {
        $adapter = new Adapter();
        $adapter->method1();
        $adapter->method2();
    }
}
Client::main();

  4.2 对象适配器

//类适配器使用的是继承
/**
 * 目标
 */
interface Target {
    public function method1(); //源类也有的方法1
    public function method2(); //源类没有的方法2
}

/**
 * 源
 */
class Source {
    public function method1() {
        echo '我是源类含有的方法一 <br>';
    }
}

/**
 * 类适配器
 */
class Adapter implements Target {
    private $_source;
    public function __construct(Source $source) {
        $this->_source = $source;
    }
    public function method1() { //委派调用source的method1方法
        $this->_source->method1();
    }
    public function method2() { //补充source中没有的method2方法
        echo '我是适配器补充的方法二'; 
    }
}
class Client {
    static public function main() {
        $adapter = new Adapter(new Source);
        $adapter->method1();
        $adapter->method2();
    }
}
Client::main(); 
//我是源类含有的方法一 
//我是适配器补充的方法二

 5、观察者模式指当一个对象的状态发生改变时,依赖他的对象会全部收到通知,并自动更新。

/**
 * 主题接口
 */
interface Subject {
    public function register(Observer $observer); //注册对象方法
    public function notify(); //通知方法
}
/**
 * 观察者接口
 */
interface Observer {
    public function update();
}
/**
 * 执行类
 */
class Action implements Subject {
    private $_observers = array();
    public function register(Observer $observer) {
        $this->_observers[] = $observer; //对象写入
    }
    public function notify() {
        foreach ($this->_observers as $observer) { //遍历对象
            $observer->update();            
        }
    }
}
/**
 * 观察者
 */
class Man implements Observer {
    public function update() {
        echo 'man观察者<br>';
    }
}
class Woman implements Observer {
    public function update() {
        echo 'woman观察者';
    }
}
$action = new Action();
$action->register(new Man);
$action->register(new Woman);
$action->notify();
原文地址:https://www.cnblogs.com/splendid/p/10225279.html