forked from Rustam-Z/python-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.py
More file actions
27 lines (20 loc) Β· 905 Bytes
/
Copy pathsingleton.py
File metadata and controls
27 lines (20 loc) Β· 905 Bytes
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
"""
Singleton restricts a class from having more than one instance and ensures a global access point to this instance.
Use case. Singleton helps
- Manage a shared resource: i.e. a single database, file manager, or printer spooler shared by multiple parts of the application.
- Store a global state (help filepath, user language, application path, etc.).
- Create a simple logger.
+ Class has a single instance
- Violates the SRP (Single Responsibility Principle).
- Itβs hard to unit test the code as the majority of test frameworks use inheritance when creating mock objects.
"""
class Singleton:
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(Singleton, cls).__new__(cls)
return cls.instance
if __name__ == "__main__":
s = Singleton()
print("Object created:", s)
s1 = Singleton()
print("Object created:", s1)