forked from davidcorne/Design-Patterns-In-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.py
More file actions
72 lines (58 loc) · 2.04 KB
/
Copy pathSingleton.py
File metadata and controls
72 lines (58 loc) · 2.04 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
#!/usr/bin/env python
# Written by: DGC
import abc
#==============================================================================
class Singleton(object):
""" A generic base class to derive any singleton class from. """
__metaclass__ = abc.ABCMeta
__instance = None
def __new__(new_singleton, *arguments, **keyword_arguments):
"""Override the __new__ method so that it is a singleton."""
if new_singleton.__instance is None:
new_singleton.__instance = object.__new__(new_singleton)
new_singleton.__instance.init(*arguments, **keyword_arguments)
return new_singleton.__instance
@abc.abstractmethod
def init(self, *arguments, **keyword_arguments):
"""
as __init__ will be called on every new instance of a base class of
Singleton we need a function for initialisation. This will only be
called once regardless of how many instances of Singleton are made.
"""
raise
#==============================================================================
class GlobalState(Singleton):
def init(self):
self.value = 0
print("init() called once")
print("")
def __init__(self):
print("__init__() always called")
print("")
class DerivedGlobalState(GlobalState):
def __init__(self):
print("derived made")
super(DerivedGlobalState, self).__init__()
def thing(self):
print(self.value)
#==============================================================================
if (__name__ == "__main__"):
d = DerivedGlobalState()
print(type(d))
d.thing()
d.value = -20
e = DerivedGlobalState()
e.thing()
f = DerivedGlobalState()
f.thing()
a = GlobalState()
# value is default, 0
print("Expecting 0, value = %i" %(a.value))
print("")
# set the value to 5
a.value = 5
# make a new object, the value will still be 5
b = GlobalState()
print("Expecting 5, value = %i" %(b.value))
print("")
print("Is a == b? " + str(a == b))