-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsingleton.py
69 lines (58 loc) · 1.48 KB
/
singleton.py
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
#!/usr/bin/python
# -*- coding : utf-8 -*-
"""
brief:
Singleton.
"""
flag = 0
if flag == 0:
# Using __dict__.
# Different objects have the same state.
class Singleton(object):
__shared_state = {}
def __init__(self):
self.__dict__ = self.__shared_state
self.state = 'Init'
def __str__(self):
return self.state
elif flag == 1:
# Using __new__.
# The same object with the same state.
class Singleton(object):
def __init__(self):
self.state = 'Init'
def __str__(self):
return self.state
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(Singleton, cls).__new__(cls)
return cls.instance
elif flag == 2:
# Using decorator.
# The same object with the same state.
def decorator(cls):
instances = {}
def getinstance(*args,**kwargs):
if cls not in instances:
instances[cls] = cls(*args,**kwargs)
return instances[cls]
return getinstance
# decorator(Singleton(object))
@decorator
class Singleton(object):
def __init__(self):
self.state = 'Init'
def __str__(self):
return self.state
if __name__ == '__main__':
obj1 = Singleton()
obj2 = Singleton()
obj1.state = 'a'
obj2.state = 'b'
print('obj1: {0}'.format(obj1))
print('obj2: {0}'.format(obj2))
obj2.state = 'c'
print('obj1: {0}'.format(obj1))
print('obj2: {0}'.format(obj2))
print('- obj1 id: {0}'.format(id(obj1)))
print('- obj2 id: {0}'.format(id(obj2)))