Home  >  Article  >  Backend Development  >  php 3种基本设计模式的组合使用_PHP教程

php 3种基本设计模式的组合使用_PHP教程

WBOY
WBOYOriginal
2016-07-13 09:46:39771browse

php 3种基本设计模式的组合使用

1.1 工厂模式,工厂方法或者类生成对象,而不是在代码直接new

<code class="language-php" hljs="">    class Factory{      
        static function getDatabase(){
            return new Mysql($host, $user, $pass);
        }
    }
    #使用
    Factory::getDatabase();
</code>

1.2 单例模式,使某个类的对象仅运行创建一个

1.有个私有的静态对象变量,专门存放本类的对象 2.有个静态的方法来创建对象 3.有个私有的构造函数,防止外部new对象 4.有个clone方法,防止clone return false
参考文章单例模式
<code class="language-php" hljs="">class Database {  
    //单一对象属性  
    private static $instance;  
    //定义一些全局变量需要存放属性  
    private $props = array();  

    //私有的构造方法  
    private function __construct(){  
        echo &#39;into construct! 该类不允许外部创建对象
        &#39;;  
    }  

    //返回单一实例  
    public static function getInstance () {  
        //判断是否已经有了实例化的对象  
        if(empty(self::$instance)) {  
            //可以被override (动态解析)  
            self::$instance = new static();  
            //不可以被override (静态解析)  
            //self::$instance = new self();  
        }  

        return self::$instance;  
    }  

    public function __clone(){
        return &#39;该类禁止clone&#39;;
    }

    //设置属性  
    public function setProperty ( $key, $value) {  
        $this->props[$key] = $value;  
    }  

    //获取属性  
    public function getPeoperty ( $key ) {  
        return $this->props[$key];  
    }  
}  

//使用 
$dbObj = Database::getInstance();  
$dbObj->setProperty(&#39;root_path&#39;,&#39;/www&#39;);  
$dbObj->setProperty(&#39;tmp_path&#39;,&#39;/tmp&#39;);  

//接下来删除该单例对象,如果还能获取到刚刚添加的属性,说明使用的是同一个对象  
unset($dbObj);  

$dbObj = Database::getInstance();  
echo $dbObj->getPeoperty(&#39;root_path&#39;);  
echo $dbObj->getPeoperty(&#39;tmp_path&#39;);  </code>

1.3 注册模式,全局共享和交换对象

1.将同一个需要多次使用对象统一注册添加别名,统一调用使用, (比如客户买一个机器肯定是去工厂认定的机构去买,而不是每个人都去工厂去买) 2.当下一次想使用一个对象的时候,不需要使用工厂,也不需要使用单例模式,直接在注册器上获取就可以了
<code class="language-php" hljs="">    class Register (){
        protected static $objects;

        function set($alias, $object){
            self::$objects[$alias] = $objects;
        }

        function get($alias){
            return self::$objects[$alias];
        }

        function _unset($alias){
            unset(self::$objects[$alias]);
        }
    }</code>

2.总结使用

<code class="language-php" hljs="">    class Factory{      
        static function getDatabase(){
            //单例模式获取数据对象
            $dbObj = Database::getInstance();
            //注册到全局树上
            Register::set(&#39;db1&#39;, $dbObj);
        }
    }
    #使用
    //第一次主文件里面
    Factory::getDatabase();
    //以后使用数据库对象直接访问
    Register::get(&#39;db1&#39;);</code>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1030445.htmlTechArticlephp 3种基本设计模式的组合使用 1.1 工厂模式 ,工厂方法或者类生成对象,而不是在代码直接new class Factory{ static function getDatabase(){ return n...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn