php—常见设计模式

工厂模式

/**
 * 工厂方法或者类生成对象,而不是在代码中直接new
 * 
 * 修改类名的时候,不需要每一个实例化语句都修改
 * 只需要修改对应的工厂方法
 * 
 * Class Factory
 * @package regina
 */
class Factory{

    static function createDatabase(){
        $db = Database::getInstance();
        return $db;
    }
}

单例模式

/**
 * 单例模式,节省资源,一个类只能实例化一个对象
 * 
 * 使某个类的对象仅允许创建一个
 * 
 * 为了实现单例模式,要私有化构造函数和clone魔术方法
 * Class Database
 * @package regina
 */
class Database{

    
    protected static $db;
    private function __construct(){

    }
    private function __clone(){

    }

    static function getInstance(){
        if (self::$db){
            return self::$db;
        }
        self::$db = new Database();
        return self::$db;
    }
}

注册模式

/**
 * 把new出来的对象保存在静态数值中,以便全局共享
 * 全局共享和交换对象
 * Class Register
 * @package regina
 */
class Register{
    protected static $array;
    static function set($name, $Obj){
        echo 'Register set run....';
        self::$array[$name] = $Obj;
    }
    static function get($name){
        echo 'Register get run....';
        return self::$array[$name];
    }

    function _unset($name){
        echo 'Register unset run....';
        unset(self::$array[$name]);
    }
}

适配器模式

/**
 * 将各种截然不同的函数接口封装成统一的API
 * Interface IDatabase
 * @package regina
 */
interface IDatabase{
    function connect($host, $username, $password, $dbname);
    function query();
    function close();
}
class Mysqli implements Database{
    protected $conn;
    function connect($host, $username, $password, $dbname){
        echo 'Mysqli connect run...';
    }
    function query(){
        echo 'Mysqli query run...';
    }
    function close(){
        echo 'Mysqli close run...';
    }
}
class Mysql implements Database{
    function connect($host, $username, $password, $dbname){
        echo 'Mysql connect run...';
    }
    function query(){
        echo 'Mysql query run...';
    }
    function close(){
        echo 'Mysql close run...';
    }
}

策略模式

/**
 * 将一组特定的行为和算法封装成类,以适应默写上下文的定义。
 * 减少模块耦合
 * Class MaleStrategy
 * @package regina\Strategy
 */
 interface UserStrategy{
    public function showAd();
    public function showcategory();
}
class MaleStrategy implements UserStrategy{
    function showAd(){
        echo 'MaleStrategy showAd run ...';
    }
    function showCategory(){
        echo 'MaleStrategy showCategory run ...';
    }
}
class FemaleStrategy implements UserStrategy{
    function showAd(){
        echo 'FemaleStrategy showAd run ...';
    }
    function showCategory(){
        echo 'FemaleStrategy showCategory run ...';
    }
}
//根据参数实例化对应的对象
class Page{
    protected $strategy;
    function index(){
        $this->strategy->showAd();
        $this->strategy->showCategory();
    }
    function setStrategy(\regina\Strategy\UserStrategy $strategy){
        $this->strategy = $strategy;
    }
}
//使用的时候才会实例化
//减少模块之间的耦合
$page = new Page;
$female = new \regina\Strategy\FemaleStrategy();
$page ->setStrategy($female);
$page ->index();

观察者模式

当一个对象发生改变时,依赖他的对象会全部收到通知

interface Observer{
    function update();
}
abstract class EventGenerator{
    private $observers = array();
    //添加观察者
    function addObserver(Observer $observer){
        $this->observers[] = $observer;
    }
    //通知
    function notify(){
        foreach($this->observers as $observer){
            $observer->update();
        }
    }
}
class Event extends \regina\EventGenerator{
    function trigger(){
        //事件发生
        echo 'Event'."<br>";
        //upodate
        $this->notify();
    }
}
class Observer implements \regina\Observer{
    public function update($event_info = null){
        echo 'update'. $event_info ."<br>";
    }
}
$event = new Event();
$event->addObserver(new Observer('对象1'));
$event->addObserver(new Observer('对象2'));
$event->trigger();


装饰器模式

/**
 * 装饰模式
 * 可以动态的给行为添加一些改变
 * 而不用改变主体逻辑
 * 减少主干代码与其他细节分支的耦合
 * Class regina
 */
 interface DrawDecorator{
    public function Befor();
    public function After();
}
class Test implements DrawDecorator{
    public $test;
    function __construct($test){
        $this->test = $test;
    }
    public function befor(){
        echo 'hello '.$this->test;
    }
    public function after(){
        echo 'goodbey '.$this->test;
    }
}
 class display {
    public function show(){
    echo '正体~~~';
    }
}
class regina extends \regina\Decorator\display {
    protected $Decorators = [];
    function AddDecorator(\regina\Decorator\DrawDecorator $decorator){
        $this->Decorators[]  = $decorator;
}
    function showDecorator(){
        foreach ($this->Decorators as $Decorator){
            $Decorator->befor();
        }
        parent::show();
        foreach ($this->Decorators as $Decorator ){
            $Decorator->after();
        }
    }
}
$rs = new regina();
$rs->AddDecorator(new \regina\Decorator\Test('regina'));
$rs->showDecorator();

挖坑待填~~~~~

 

posted @ 2016-10-30 23:26  罗茜  阅读(440)  评论(0编辑  收藏  举报