-
Notifications
You must be signed in to change notification settings - Fork 152
/
index.ts
81 lines (73 loc) · 2.78 KB
/
index.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* EN: Singleton Design Pattern
*
* Intent: Lets you ensure that a class has only one instance, while providing a
* global access point to this instance.
*
* RU: Паттерн Одиночка
*
* Назначение: Гарантирует, что у класса есть только один экземпляр, и
* предоставляет к нему глобальную точку доступа.
*/
/**
* EN: The Singleton class defines an `instance` getter, that lets clients
* access the unique singleton instance.
*
* RU: Класс Одиночка определяет геттер `instance`, который позволяет
* клиентам получить доступ к уникальному экземпляру одиночки.
*/
class Singleton {
static #instance: Singleton;
/**
* EN: The Singleton's constructor should always be private to prevent
* direct construction calls with the `new` operator.
*
* RU: Конструктор Одиночки всегда должен быть скрытым, чтобы предотвратить
* создание объекта через оператор new.
*/
private constructor() { }
/**
* EN: The static getter that controls access to the singleton instance.
*
* This implementation allows you to extend the Singleton class while
* keeping just one instance of each subclass around.
*
* RU: Статический геттер, управляющий доступом к экземпляру одиночки.
*
* Эта реализация позволяет вам расширять класс Одиночки, сохраняя повсюду
* только один экземпляр каждого подкласса.
*/
public static get instance(): Singleton {
if (!Singleton.#instance) {
Singleton.#instance = new Singleton();
}
return Singleton.#instance;
}
/**
* EN: Finally, any singleton can define some business logic, which can
* be executed on its instance.
*
* RU: Наконец, любой одиночка может содержать некоторую бизнес-логику,
* которая может быть выполнена на его экземпляре.
*/
public someBusinessLogic() {
// ...
}
}
/**
* EN: The client code.
*
* RU: Клиентский код.
*/
function clientCode() {
const s1 = Singleton.instance;
const s2 = Singleton.instance;
if (s1 === s2) {
console.log(
'Singleton works, both variables contain the same instance.'
);
} else {
console.log('Singleton failed, variables contain different instances.');
}
}
clientCode();