One article teaches you to distinguish process-oriented and object-oriented programming

(For the complete pdf version of the original text of this article, please follow the official account of "Zhang Zhangxue Algorithm" and reply "006" to receive~)

1. Programming ideas

    The essence of programming is logic . If you want to learn programming well, you must not only master the basic skills - programming language, but also cultivate good programming ideas. Process-oriented and object-oriented are two important programming ideas . Data structures and algorithms are programming skills, not programming ideas.

    The principle of excellent code: high reuse, low coupling . These two principles ensure high code flexibility, easy modification, and high reusability, which can improve code writing efficiency.

2. Process-oriented

2.1 Process-oriented definition

    Process-oriented is the thinking of most programmers. It focuses on the process, analyzes the steps to solve the problem in turn, and encapsulates them with functions, and then calls the corresponding functions in the main function according to specific steps.

    The program body of process-oriented programming is a function, and a function is an encapsulated module. Each sub-step is often completed through each function. Therefore, process-oriented programming is centered on behavior (function), and always focuses on how to solve problems step by step . , so as to realize the sequential execution of functions. The traditional process-oriented programming idea can be summed up in eight words- top-down, gradually refined , it describes the function to be realized as a continuous step (process) from the beginning to the end, and completes these steps step by step. step.

2.2 Advantages and disadvantages

Three, object-oriented

3.1 Object-oriented definition

    When the scale of the problem becomes very large, it is far from enough to use process-oriented thinking, so object-oriented programming thinking appears.

    Object-oriented programming is a programming idea centered on data (objects). It decomposes the problem to be solved into each object (rather than each step) . For example, there are many people and things in the world, and each person and thing is an independent object. , have their own attributes and behaviors, and interact with objects through methods. The purpose of establishing an object is not to complete a step, but to describe the properties and behavior of an object in the steps of solving the entire problem.

The steps of analyzing the problem in object-oriented programming: analyze which entities     are involved in the problem , what attributes and methods these entities should have , and how we can solve the problem by calling the attributes and methods of these entities . In the real world, any operation or business logic implementation needs an entity to complete. The entity is the ruler of the action . Without the entity, there will be no action.

    Object-oriented programming is a relatively advanced and abstract idea. It has three characteristics and five principles : encapsulation, inheritance, polymorphism, singleness, opening and closing, Liskov substitution, dependency inversion, and interface isolation.

    Object-oriented thinking is the embodiment of good code principles:

  • Object-oriented inheritance is the embodiment of high reuse;
  • Object-oriented makes process-oriented variables and functions limited to the object and becomes part of the object, so that variables and functions have a separate execution environment, so that variables and functions are isolated from the outside world, which is the embodiment of low coupling.

    How to cultivate object-oriented programming thinking?

  • Always keep in mind the three characteristics and five principles of object-oriented;
  • Constantly refactor the already written business logic code.

3.2 Advantages and disadvantages

3.3 The difference between process-oriented and object-oriented

  • Process-oriented is suitable for developing small and medium-sized projects, and object-oriented is suitable for developing large-scale projects;
  • The most essential difference between object-oriented and process-oriented lies in the different starting points for considering problems. Process-oriented is based on the event flow as the starting point for considering the problem, while object-oriented is based on the role (object) participating in the event as the starting point for considering the problem, so object-oriented is more flexible when dealing with problems;
  • For process-oriented thinking: when it is necessary to realize a function, what is important is the development steps and process, and each step needs to be done by oneself. For object-oriented thinking: When it is necessary to realize a function, what is important is not the process and steps, but the object, and what behavior each object performs.

4. A small example

4.1 Example 1

    Taking the radius of a circle and finding its circumference and area as an example, understand the difference between process-oriented and object-oriented programming.

    The idea of ​​process-oriented analysis is to define two functions for calculating the circumference and area respectively. The input of these two functions is the area of ​​a circle, and the two functions can be called in turn in the main function.

def cal_round(para):
    pass
    
def cal_area(para):
    pass
    
    
if __name__ == '__main__':
    r = 1
    round = cal_round(r)
    area = cal_area(r)

    The idea of ​​object-oriented analysis is : this problem has an object/class (circle), the object/class has an attribute (radius), and the object/class has two methods (finding circumference, area), so after constructing the circle class, use The radius can initialize this class.

class circle(object):
    def __init__(self, r):
        self.r = r

    def cal_round(self):
        round = 2 * 3.14 * self.r
        return round

    def cal_area(self):
        area = 3.14 * self.r * self.r
        return area


if __name__ == '__main__':
    r = 2
    cir = circle(r)
    round = cir.cal_round()
    area = cir.cal_area()

Guess you like

Origin blog.csdn.net/weixin_40583722/article/details/127622199