PHP设计模式

php

浏览数:409

2019-1-7

片段 1片段 2片段 3片段 4片段 5


单例模式

<?php
//单例模式

class Singleton
{
    private $runtime;
    private static $instance;

    private function __construct() {
        $this->runtime = time();
    }

    public static function getInstance() {
        if(!(self::$instance instanceof self)){
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function run() {
        echo $this->runtime,PHP_EOL;
    }

    private function __clone() {
    }

    public function __destruct() {
    }
}

$a = Singleton::getInstance();
$a->run();

$b = Singleton::getInstance();
$b->run();

$c = Singleton::getInstance();
$c->run();


工厂模式

<?php
//工厂模式

abstract class Operation {
    abstract public function getValue($num1, $num2);
}

class OperationAdd extends Operation {
    public function getValue($num1, $num2) {
        return $num1 + $num2;
    }
}

class OperationSub extends Operation {
    public function getValue($num1, $num2) {
        return $num1 - $num2;
    }
}

class Factory {
    public static function createObj($operate) {
        switch ($operate) {
            case '+':
                return new OperationAdd();
                break;

            case '-':
                return new OperationSub();
                break;
        }
    }
}

$test = Factory::createObj('+');
echo $test->getValue(2, 3);


注册模式

<?php
//注册模式

//创建单例
class Single{
    public $hash;
    static protected $ins=null;
    final protected function __construct(){
        $this->hash=rand(1,9999);
    }

    static public function getInstance(){
        if (self::$ins instanceof self) {
            return self::$ins;
        }
        self::$ins=new self();
        return self::$ins;
    } 
}

//工厂模式
class RandFactory{
    public static function factory(){
        return Single::getInstance();
    }
}

//注册树
class Register{
    protected static $objects;
    public static function set($alias,$object){
        self::$objects[$alias]=$object;
    }
    public static function get($alias){
        return self::$objects[$alias];
    }
    public static function _unset($alias){
        unset(self::$objects[$alias]);
    }
}

Register::set('rand',RandFactory::factory());

$object=Register::get('rand');

print_r($object);


适配器模式

<?php
//适配器模式

//目标角色  
interface Target {  
    public function simpleMethod1();
    public function simpleMethod2();
}

//源角色
class Adaptee {
    public function simpleMethod1(){
        echo 'Adapter simpleMethod1'."<br>";
    }
}

//类适配器角色
class Adapter implements Target {
    private $adaptee;

    function __construct(Adaptee $adaptee) {
        $this->adaptee = $adaptee;
    }

    //委派调用Adaptee的sampleMethod1方法
    public function simpleMethod1(){
        echo $this->adaptee->simpleMethod1();
    }

    public function simpleMethod2(){
        echo 'Adapter simpleMethod2'."<br>";
    }   
}

//客户端
class Client {

    public static function main() {
        $adaptee = new Adaptee();
        $adapter = new Adapter($adaptee);
        $adapter->simpleMethod1();
        $adapter->simpleMethod2();
    }
}

Client::main();


观察者模式

<?php
//观察者模式

// 主题接口
interface Subject{
    public function register(Observer $observer);
    public function notify();
}

// 观察者接口
interface Observer{
    public function watch();
}

// 主题
class Action implements Subject{
    public $_observers=array();

    public function register(Observer $observer){
        $this->_observers[]=$observer;
    }

    public function notify(){
        foreach ($this->_observers as $observer) {
            $observer->watch();
        }

    }
 }

// 观察者
class Cat implements Observer{
    public function watch(){
        echo "Cat watches TV<hr/>";
    }
}

class Dog implements Observer{
    public function watch(){
        echo "Dog watches TV<hr/>";
    }
}

class People implements Observer{
    public function watch(){
        echo "People watches TV<hr/>";
    }
}

// 应用实例
$action=new Action();
$action->register(new Cat());
$action->register(new People());
$action->register(new Dog());
$action->notify();