-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode03_singleton.py
More file actions
87 lines (60 loc) · 1.4 KB
/
Copy pathcode03_singleton.py
File metadata and controls
87 lines (60 loc) · 1.4 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
79
80
81
82
83
84
85
86
87
# __new__()实现单例模式
class DateUtil(object):
ins = None
def __new__(cls, *args, **kwargs):
if cls.ins is None:
cls.ins = super().__new__(cls)
return cls.ins
d1 = DateUtil()
d2 = DateUtil()
print(id(d1))
print(id(d2))
# 通过类方法实现单例模式
class JsonUtil(object):
instance = None
@classmethod
def get_instance(cls):
if cls.instance is None:
cls.instance = super().__new__(cls)
return cls.instance
j1 = JsonUtil.get_instance()
j2 = JsonUtil.get_instance()
print(id(j1))
print(id(j2))
# 通过装饰器实现单例模式
def outer(fn):
_ins = {}
def inner():
if fn not in _ins:
_ins[fn] = fn()
return _ins[fn]
return inner
@outer
class XxxUtil(object):
pass
xx1 = XxxUtil()
xx2 = XxxUtil()
print(id(xx1))
print(id(xx2))
print("*" * 50)
# 通过导入模块时实现
from tools.time_tools import DateUtil
from tools.time_tools import DateUtil
ddd = DateUtil()
print(ddd.__module__)
print(ddd.__class__)
# 通过元类实现
class A(object):
def __new__(cls, *args, **kwargs):
if not hasattr(cls, "ins"):
cls.ins = super().__new__(cls)
return cls.ins
a1 = A()
a2 = A()
print(id(a1))
print(id(a2))
# __call__使用
class B(object):
def __call__(self, *args, **kwargs):
print("这是call方法")
B()() # 这是call方法