首页 > 其他 > 详细

单例模式 Singleton

时间:2017-11-05 12:52:25      阅读:198      评论:0      收藏:0      [点我收藏+]

保证一个类仅有一个实例,并提供一个访问它的全局访问点.

UML:

技术分享

 

class A{
    private static $instance = null;
 
    private function __contruct(){}
 
    public static getInstance()
    {
    if (is_null(self::$instance)) {
            self::$instance = new static(); //此处注意和new self的区别
        }
        return self::$instance;
    }
}
 
调用:
$a = A::getInstance();
$a->getInstance();

  

 

单例模式 Singleton

原文:http://www.cnblogs.com/itfenqing/p/7787123.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!