首页 > 其他 > 详细

设计模式 单例模式

时间:2020-05-26 21:51:41      阅读:40      评论:0      收藏:0      [点我收藏+]

转自:https://github.com/domnikl/DesignPatternsPHP

 1 final class Singleton
 2 {
 3     private static ?Singleton $instance = null;
 4 
 5     public static function getInstance(): Singleton
 6     {
 7         if (static::$instance === null) {
 8             static::$instance = new static();
 9         }
10 
11         return static::$instance;
12     }
13 
14     private function __construct()
15     {
16     }
17 
18     private function __clone()
19     {
20     }
21 
22     private function __wakeup()
23     {
24     }
25 }

 

 1 class SingletonTest extends TestCase
 2 {
 3     public function testUniqueness()
 4     {
 5         $firstCall = Singleton::getInstance();
 6         $secondCall = Singleton::getInstance();
 7 
 8         $this->assertInstanceOf(Singleton::class, $firstCall);
 9         $this->assertSame($firstCall, $secondCall);
10     }
11 }

 

设计模式 单例模式

原文:https://www.cnblogs.com/ts65214/p/12968425.html

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