单例模式

注意:单例模式被视为一种「反模式」。为了更好的可测试性和可维护性,请使用依赖注入。

目的

在应用程序中,一个对象只有一个用于处理所有请求的实例。

使用场景

  • 数据库连接器
  • 日志记录
  • 为应用锁定文件(这是文件系统中的唯一场景)

UML 类图

单例模式类图

代码

Singleton.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php declare(strict_types = 1);

namespace DesignPatterns\Creational\Singleton;

final class Singleton
{
private static ?Singleton $instance = null;

/**
* gets the instance via lazy initialization (created on first usage)
*/
public static function getInstance(): Singleton
{
if (static::$instance === null) {
static::$instance = new static();
}

return static::$instance;
}

/**
* is not allowed to call from outside to prevent from creating multiple instances,
* to use the singleton, you have to obtain the instance from Singleton::getInstance() instead
*/
private function __construct()
{
}

/**
* prevent the instance from being cloned (which would create a second instance of it)
*/
private function __clone()
{
}

/**
* prevent from being unserialized (which would create a second instance of it)
*/
private function __wakeup()
{
}
}

测试

Tests/SingletonTest.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php declare(strict_types = 1);

namespace DesignPatterns\Creational\Singleton\Tests;

use DesignPatterns\Creational\Singleton\Singleton;
use PHPUnit\Framework\TestCase;

class SingletonTest extends TestCase
{
public function testUniqueness()
{
$firstCall = Singleton::getInstance();
$secondCall = Singleton::getInstance();

$this->assertInstanceOf(Singleton::class, $firstCall);
$this->assertSame($firstCall, $secondCall);
}
}