zoukankan      html  css  js  c++  java
  • 设计模式-PHP实现单件模式的几种方式

    单件模式是我们在开发中经常用到的一种设计模式,利用PHP5面向对象的特性,我们可以很容易的构建单件模式的应用,下面是单件模式在PHP中的几种实现方法:

    class Stat{
        
    static $instance = NULL;
        
        
    static function getInstance(){
            
    if(self::$instance == NULL){
                self
    ::$instance = new Stat();
            }
            
            
    return self::$instance;
        }
        
        
    private function __construct(){
        }
        
        
    private function __clone(){
        }    
        
        
    function sayHi(){
            
    return "The Class is saying hi to u ";
        }
    }


    echo Stat::getInstance()->sayHi();

    这是一种最通常的方式,在一个getInstance方法中返回唯一的类实例。

    对这里例子稍加修改,便可以产生一个通用的方法,只要叫道任何你想用到单件的类里,就可以了。

    class Teacher{
        
    function sayHi(){
            
    return "The teacher smiling and said 'Hello '";
        }
        
        
    static function getInstance(){
            
    static $instance;
            
            
    if(!isset($instance)){
                
    $c = __CLASS__;
                
    $instance = new $c;
            }
            
            
    return $instance;
        }
    }

    echo Teacher::getInstance()->sayHi();

    最后一种是提供一个singleton类,然后通过调用getInstance方法,可以为任何一个类生产出一个实例来。

    class singleton{
        
    function getInstance($class){
            
    static $instances = array();
            
    if(!array_key_exists($class,$instances)){
                
    $instances[$class= &new $class;
            }
            
    $instance = $instances[$class];
            
            
    return $instance;
        }
    }

    class People{
        
    function sayHi(){
            
    return 'Hello i am a people?';
        }
    }

    echo "<br />";
    echo singleton::getInstance('People')->sayHi();

    通过这三种方法,我们可以很容易的应用单件模式,如果能够结合工厂模式,将使我们的编程变得更有条理和效率。

  • 相关阅读:
    POJ 2778(自动机+矩阵幂乘(字符串果然是个坑爹玩应!))
    CF357D(规律题,不好想出来!)
    HDU 4107(线段树 特殊懒惰标记)g++ TLE,c++才过(呜呜呜呜)
    hdu 3954(线段树的特殊lazy操作-更新时才遍历!)
    CF354C(思路+(左区间++,1+右区间--思想))
    hdu3851(dp+区间范围很大且有循环的处理方法)
    CF119D(字符串-哈希求解(KMP求了半天,结果哈希更简单!))
    CF 353D
    HDU 4760 字典树(题意比较难理解)
    hdu3649(取石子博弈+dp)
  • 原文地址:https://www.cnblogs.com/cocowool/p/1415660.html
Copyright © 2011-2022 走看看