SOLIP設(shè)計(jì)原則
1学少、單一責(zé)任原則(SRP)
一個(gè)對(duì)象對(duì)只應(yīng)該為一個(gè)元素負(fù)責(zé)
2、開放封閉原則(OCP)
對(duì)擴(kuò)展開放秧骑,修改封閉
3版确、里氏替換原則(LSP)
可以使用任何派生類替換基類
4、接口分離原則(ISP)
對(duì)于接口進(jìn)行分類避免一個(gè)接口的方法過多
5乎折、依賴倒置原則(DIP)
隔離關(guān)系绒疗,使用接口或抽象類代指
6、依賴注入(DI)和控制反轉(zhuǎn)原則(ICO)
使用鉤子再原來執(zhí)行流程中注入其他對(duì)象
接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# =================================================以下是接口
classIorderRepository:##接口
deffetch_one_by(self,nid):
'''
獲取單條數(shù)據(jù)的方法骂澄,所有的繼承呢當(dāng)前類的類必須繼承
:param nid:
:return:
'''
# raise Exception('子類中必須包含該方法')
classOrderReposititory(IorderRepository):#類
deffetch_one_by(self,nid):
print(nid)
obj=OrderReposititory()
obj.fetch_one_by(1)
抽象類抽象方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21importabc
classFoo(metaclass=abc.ABCMeta):##抽象類
deff1(self):
print(123)
deff2(self):
print(456)
@abc.abstractmethod##抽象方法
deff3(self):
'''
???
:return:
'''
classBar(Foo):
deff3(self):
print(33333)
b=Bar()
b.f3()
引入依賴注入
解釋器解釋類的流程
1
2
3
4
5
6
7
8
9
10
11
12
13
14# ======================================解釋器解釋類的流程===================
#? 解釋器解釋:
# class Foo:
#???? def __init__(self):
#???????? self.name =123
#???? def f1(self):
#???????? print(self.name)
# 1.遇到class Foo,執(zhí)行type的__init__方法
# 2.type的init的方法做什么呢吓蘑!不知道
# obj =Foo()
# obj.f1()
# 3.執(zhí)行Type的__call__方法
# 執(zhí)行Foo類的__new__方法
# 執(zhí)行Foo類的__init__方法
依賴注入在什么之前做什么操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20classMyType(type):
def__call__(cls,*args,**kwargs):##執(zhí)行Type的__call__方法,這里的cls就是<__main__.Foo object at 0x001B59F0> Foo類
obj=cls.__new__(cls,*args,**kwargs)##Foo的__new__方法
print('-------------')
obj.__init__(*args,**kwargs)##在執(zhí)行Foo的__init__的之前做什么操作
returnobj
classFoo(metaclass=MyType):
def__init__(self, name):
print('============')
self.name=name
deff1(self):
print(self.name)
obj=Foo(123)
print(obj)
print(obj.name)
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#=================================依賴注入案例一======================================
classMyType(type):
def__call__(cls,*args,**kwargs):##執(zhí)行Type的__call__方法坟冲,這里的cls就是<__main__.Foo object at 0x001B59F0> Foo類
obj=cls.__new__(cls,*args,**kwargs)##Foo的__new__方法
ifcls==Foo1:
obj.__init__(Foo())
elifcls==Foo2:
obj.__init__(Foo1())
returnobj
classFoo(metaclass=MyType):
def__init__(self, args):
print('============')
self.name=args
deff(self):
print(self.name)
classFoo1(metaclass=MyType):
def__init__(self, args):
print('============')
self.name=args
deff1(self):
print(self.name)
classFoo2(metaclass=MyType):
def__init__(self, args):
print('============')
self.name=args
deff2(self):
print(self.name)
obj=Foo2()
obj.f2()
# <__main__.Foo1 object at 0x002DA4F0>
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#######################依賴注入案例二====================================================
#
# class Mapper:
#???? __mapper_relation = {}
#
#???? @staticmethod
#???? def register(cls, value):
#???????? Mapper.__mapper_relation[cls] = value
#
#???? @staticmethod
#???? def exist(cls):?? ###判斷是否在里面
#???????? if cls in Mapper.__mapper_relation:
#???????????? return True
#???????? return False
#
#???? @staticmethod
#???? def value(cls):
#???????? return Mapper.__mapper_relation[cls]
#
#
# class MyType(type):
#???? def __call__(cls, *args, **kwargs):? ##執(zhí)行Type的__call__方法磨镶,這里的cls就是<__main__.Foo object at 0x001B59F0> Foo類
#???????? obj = cls.__new__(cls, *args, **kwargs)? ##Foo的__new__方法
#???????? arg_list = list(args)
#???????? if Mapper.exist(cls):
#???????????? value = Mapper.value(cls)
#???????????? arg_list.append(value)
#???????? obj.__init__(*arg_list, **kwargs)? ##在執(zhí)行Foo的__init__的之前做什么操作
#???????? return obj
#
#
# class Foo(metaclass=MyType):
#???? def __init__(self, name):
#???????? self.name = name
#
#???? def f1(self):
#???????? print(self.name)
#
#
# class Bar(metaclass=MyType):
#???? def __init__(self, name):
#???????? self.name = name
#
#???? def f1(self):
#???????? print(self.name)
#
#
# Mapper.register(Foo, '666')
# Mapper.register(Bar, '999')
# obj = Foo()
#
# print(obj)
# print(obj.name)
# b = Bar()
# print(b.name)
# <__main__.Foo object at 0x00583810>
# 666
# 999