#the principle of object-oriented adherence: SOLID#S (Single Responsibility Principle)#single principle of responsibility#A class is responsible for only one responsibility#Benefits#easy to maintain, write high cohesion code#Easy Code Reuse#Case#we previously wrote the calculator class#assuming that only subtraction operations and result printing are included#If you need to add later, the result is written to the file, the result participates in other operations, the result uploads the server ...#Do you need to modify this class every time? It makes this class very bloated; not conducive to maintenance and partial code reuse#Solution Solutions#Calculator contains only, subtraction operations and results returned#spread the processing of results to other appropriate categories, depending on the situation #O (Open Closed Principle) #Open Closure Principle #Open for expansion #Close for modification #Easy maintenance for code security and Extensibility #Case #we wrote before, in the person class, the way that the puppy kitten works #It is assumed that the working method name of the puppy kitten is watch and catch, respectively; #The method of letting the pet work in the person class needs to be judged by multiple branches through if #If the new pet class is extended later, then the method of making the pet work in the person class must be modified accordingly . #Solution Solutions #the unification of methods through the base class, as well as the abstract class. #O (Open Closed Principle) #Open Closure Principle #Open for expansion #Close for modification #Easy maintenance for code security and Extensibility #Case #we wrote before, in the person class, the way that the puppy kitten works #It is assumed that the working method name of the puppy kitten is watch and catch, respectively; #The method of letting the pet work in the person class needs to be judged by multiple branches through if #If the new pet class is extended later, then the method of making the pet work in the person class must be modified accordingly . #Solution Solutions #the unification of methods through the base class, as well as the abstract class. #L (Liskov Substitution Principle) L (Liskov Substitution Principle) #I (Interface segregation Principle) #Interface Separation Principle #if a class contains too many interface methods that are not "indivisible" in the process of use, then they should be detached #The so-called interface, in Python, can be simply understood as "abstract method" #Benefits #improve the reuse value of interfaces #Case #Bird #eat, bark, fly #The design of the above interface design is unreasonable under the condition of ostrich class. #D (Dependency inversion Principle) #Dependency Inversion principle #high-rise modules should not be directly dependent on low-level modules #they should rely on abstract classes or interfaces #Benefits #facilitates code maintenance #Case #Computer class, depends on not a specific mouse class #and should be, the abstract of the mouse class #Can click, can double-click, can right click, can move the mouse pointer ... #It can be a touchpad, a wired mouse, or a Bluetooth mouse ...
Design principles for Python classes