Python abstract classes, abstract methods, interfaces, dependency injection, Solip

Source: Internet
Author: User

1. Procedure Design principle: Solip

Solip Design Principles
1. Single responsibility principle (SRP)
An object pair should only be responsible for one element
2. Open closure principle (OCP)
Open for extension, modify closed
3. The Richter Replacement principle (LSP)
You can replace a base class with any derived class
4. Interface separation principle (ISP)
There are too many ways to classify interfaces to avoid an interface
5. Dependency inversion principle (DIP)
Isolate relationships, using interfaces or abstract classes to refer to
6. Dependency Injection (DI) and control reversal principle (ICO)
Use hooks to inject other objects into the original execution flow

Interface:
# ================================================= The following is the interface class Iorderrepository:  # #接口    def fetch_one_by ( Self,nid):        "The        method to get a single data, all the inheritance of the class of the current class must inherit        :p Aram nid:        : return:        " '        # Raise Exception (' The subclass must contain the method ') class Orderreposititory (iorderrepository): #类    def fetch_one_by (self,nid):        print (nid) obj = Orderreposititory () obj.fetch_one_by (1)
Abstract class abstract methods
Import Abcclass Foo (METACLASS=ABC. Abcmeta):  # #抽象类    def F1 (self):        print (123)    def f2 (self):        print (456)    @abc. Abstractmethod  # #抽象方法    def f3 (self):        "        ???        : Return:" '        class Bar (Foo):    def f3 (self):        print (33333) b = Bar () b.f3 ()

  

Introducing Dependency Injection

The interpreter interprets the process of the class

# ====================================== Interpreter explains the process of the class ===================# interpreter  : # class foo:#     def __init__ ( Self): #         self.name =123#     def F1 (self): #         print (self.name) # 1. When you encounter class Foo, execute the type's __init__ method # 2. What do you do with the Init method of type? Don't know # obj =foo () # OBJ.F1 () # 3. Execute the type's __call__ method # execute the Foo class's __new__ method # Execute the __init__ method of the Foo class

  

What to do before dependency injection
Class MyType (Type):    def __call__ (CLS, *args, **kwargs):  # #执行Type的__call__方法, the CLS here is <__main__. Foo Object at 0x001b59f0> foo class        obj = cls.__new__ (CLS, *args, **kwargs)  # #Foo的__new__方法        print ('--------- ----')        obj.__init__ (*args, **kwargs)  # #在执行Foo的__init__的之前做什么操作        return objclass Foo (metaclass=mytype):    def __init__ (self, name):        print (' ============ ')        self.name = name    def f1 (self):        print ( Self.name) obj = Foo (123) print (obj) print (obj.name)

  

#================================= Dependency Injection Case one ======================================class MyType (type):    def __ Call__ (CLS, *args, **kwargs):  # #执行Type的__call__方法, the CLS here is <__main__. Foo Object at 0x001b59f0> foo class        obj = cls.__new__ (CLS, *args, **kwargs)  # #Foo的__new__方法        If cls = = foo1:
   
    obj.__init__ (Foo ())        elif cls = = Foo2:            obj.__init__ (Foo1 ())        return objclass Foo (metaclass=mytype):    def __init__ (self, args):        print (' ============ ')        self.name = args    def f (self):        print (Self.name) Class Foo1 (Metaclass=mytype):    def __init__ (self, args):        print (' ============ ')        self.name = args    def F1 (self):        print (Self.name) class Foo2 (Metaclass=mytype):    def __init__ (self, args):        print (' ====== ====== ')        self.name = args    def f2 (self):        print (self.name) obj = Foo2 () obj.f2 () # <__main__. Foo1 Object at 0x002da4f0>
   

  

###################### #依赖注入案例二 ====================================================## class Mapper:# __mapper_ Relation = {}## @staticmethod # def register (CLS, value): # Mapper.__mapper_relation[cls] = value## @sta         ticmethod# 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): # #执行Type的__call__方法, the CLS here is <__main__. Foo Object at 0x001b59f0> foo class # obj = cls.__new__ (CLS, *args, **kwargs) # #Foo的__new__方法 # arg_list = Li         St (args) # if Mapper.exist (CLS): # value = Mapper.value (CLS) # arg_list.append (value) # Obj.__init__ (*arg_list, **kwargs) # #在执行Foo的__init__的之前做什么操作 # return obj### class Foo (Metaclass=mytype): # de F __init__ (self, name): # self.name = name## def F1 (self): #         Print (Self.name) # # # # class Bar (Metaclass=mytype): # def __init__ (self, name): # self.name = name## D EF F1 (self): # print (self.name) # # Mapper.register (Foo, ' 666 ') # Mapper.register (Bar, ' 999 ') # obj = Foo () # # print (ob j) # Print (obj.name) # b = Bar () # Print (b.name) # <__main__. Foo object at 0x00583810># 666# 999

  

Python abstract classes, abstract methods, interfaces, dependency injection, Solip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.