1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 ###########接口###################### 5 # class Iorderddd(): 6 # def fetch_one_by(self,nid): 7 # raise Exception(‘子类必须实现该方法‘) 8 # 9 # class aaa(Iorderddd): 10 # def fetch_one_by(self, nid): 11 # print(nid) 12 # aa = aaa() 13 # aa.fetch_one_by(1) 14 15 ###########抽象类,抽象方法###################### 16 # import abc 17 # class Foo(metaclass=abc.ABCMeta): 18 # def f1(self): 19 # print(‘123‘) 20 # 21 # @abc.abstractmethod 22 # def f2(self): 23 # ‘‘‘ 24 # 25 # :return: 26 # ‘‘‘ 27 # 28 # class Bar(Foo): 29 # def f2(self): 30 # print(‘3333‘) 31 # 32 # b = Bar() 33 # b.f2() 34 ###########组合###################### 35 # class Foo: 36 # def f1(self): 37 # pass 38 # def f2(self): 39 # pass 40 # 41 # class SqlHelper: 42 # def __init__(self,f): 43 # self.f = f 44 # def fetch_one(self): 45 # self.f.f1() 46 # def fetch_all(self): 47 # pass 48 # class UserInfo: 49 # def __init__(self,helper): 50 # self.s = helper 51 # def login(self): 52 # self.s.fetch_one() 53 # def loginout(self): 54 # self.s.fetch_one() 55 # def userlist(self): 56 # self.s.fetch_all() 57 # f = Foo() 58 # b = SqlHelper(f) 59 # obj = UserInfo(b) 60 # obj.login() 61 # 类作为参数传入到另一个类中,降低耦合 62 63 ########## 类创建的本质###################### 64 # class Foo: 65 # def __init__(self): 66 # self.name = 123 67 # def f1(self): 68 # print(‘f1‘) 69 # # 解释器执行步骤 70 # # 1、遇到class Foo 执行Type的__init__方法 71 # # 2、type的init方法。。。 72 # obj = Foo() 73 # 3、执行type的__call__方法 74 # 执行Foo类的__new__方法 75 # 执行Foo类的__init__方法 76 77 ########## 自定义type###################### 78 # class MyType(type): 79 # def __call__(cls, *args, **kwargs): 80 # obj = cls.__new__(cls,*args,**kwargs) 81 # print(‘==========‘) 82 # obj.__init__(*args,**kwargs) 83 # return obj 84 # class Foo(metaclass=MyType): 85 # def __init__(self,name): 86 # print(‘---------‘) 87 # self.name = name 88 # def f1(self): 89 # print(self.name) 90 # obj = Foo(123) 91 # print(obj) 92 # print(obj.name) 93 ########## 依赖注入###################### 94 class Mapper: 95 __mapper_relation ={} 96 @staticmethod 97 def register(cls,value): 98 Mapper.__mapper_relation[cls]=value 99 @staticmethod 100 def exist(cls): 101 if cls in Mapper.__mapper_relation: 102 return True 103 return False 104 @staticmethod 105 def value(cls): 106 return Mapper.__mapper_relation[cls] 107 class MyType(type): 108 def __call__(cls, *args, **kwargs): 109 obj = cls.__new__(cls,*args,**kwargs) 110 arg_list = list(args) 111 if Mapper.exist(cls): 112 value = Mapper.value(cls) 113 arg_list.append(value) 114 obj.__init__(*arg_list,**kwargs) 115 return obj 116 class Foo(metaclass=MyType): 117 def __init__(self,name): 118 self.name = name 119 def f1(self): 120 print(self.name) 121 122 class Bar(metaclass=MyType): 123 def __init__(self,name): 124 self.name = name 125 def f1(self): 126 print(self.name) 127 Mapper.register(Foo,‘666‘) 128 Mapper.register(Bar,‘999‘) 129 f = Foo() 130 print(f.name) 131 b= Bar() 132 print(b.name) 133 ##############程序设计原则############# 134 ‘‘‘SOLIP设计原则 135 1、单一责任原则(SRP) 136 一个对象对只应该为一个元素负责 137 2、开放封闭原则(OCP) 138 对扩展开放,修改封闭 139 3、里氏替换原则(LSP) 140 可以使用任何派生类替换基类 141 4、接口分离原则(ISP) 142 对于接口进行分类避免一个接口的方法过多 143 5、依赖倒置原则(DIP) 144 隔离关系,使用接口或抽象类代指 145 6、依赖注入(DI)和控制反转原则(ICO) 146 使用钩子再原来执行流程中注入其他对象 147 ‘‘‘
时间: 2024-10-22 18:38:54