-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsingleton.py
More file actions
79 lines (57 loc) · 1.7 KB
/
singleton.py
File metadata and controls
79 lines (57 loc) · 1.7 KB
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
"""
Singleton Design Pattern
The Singleton pattern ensures a class has only one instance and provides
a global point of access to it.
Use cases:
- Database connections
- Logger instances
- Configuration managers
- Cache managers
"""
class Singleton:
"""Singleton implementation using __new__ method."""
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(Singleton, cls).__new__(cls)
return cls._instance
def __init__(self):
if not hasattr(self, 'initialized'):
self.value = None
self.initialized = True
def set_value(self, value):
"""Set a value."""
self.value = value
def get_value(self):
"""Get the value."""
return self.value
# Alternative: Using decorator
def singleton(cls):
"""Singleton decorator."""
instances = {}
def get_instance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return get_instance
@singleton
class Config:
"""Example singleton using decorator."""
def __init__(self):
self.settings = {}
# Example usage
if __name__ == "__main__":
# Test basic singleton
s1 = Singleton()
s1.set_value("Hello")
s2 = Singleton()
print(f"s1 value: {s1.get_value()}")
print(f"s2 value: {s2.get_value()}")
print(f"Same instance: {s1 is s2}")
# Test decorator singleton
c1 = Config()
c1.settings['key'] = 'value'
c2 = Config()
print(f"\nc1 settings: {c1.settings}")
print(f"c2 settings: {c2.settings}")
print(f"Same instance: {c1 is c2}")