-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSingletonPattern.py
More file actions
30 lines (24 loc) · 882 Bytes
/
Copy pathSingletonPattern.py
File metadata and controls
30 lines (24 loc) · 882 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
28
29
30
"""
Singleton Pattern - Simplest pattern of all that ensures only one class
has only one instance and it is a global property.
Example - Usually for configuration settings where you don't want to create
a chaos by having many instances of the same class
The example below referenced from Python's documentation:
https://www.python.org/download/releases/2.2/descrintro/#__new__
"""
class Singleton(object):
def __new__(cls, *args, **kwds):
it = cls.__dict__.get("__it__")
if it is not None:
return it
cls.__it__ = it = object.__new__(cls)
it.init(*args, **kwds)
return it
def init(self, *args, **kwds):
pass
class ConfigModule(Singleton):
def someReallyUsefulFunction(self):
print "Hello World"
instance1 = ConfigModule()
instance2 = ConfigModule()
assert instance1 is instance2