Design principles for Python classes

Source: Internet
Author: User

#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

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.