Comparison of "Process-Oriented Development" and "Object-Oriented Approaches"

1. Introduction to knowledge points

     1. Concept description

1) Process-oriented : It is an event-centered programming idea. When programming, the steps to solve the problem are analyzed, and then functions are used to implement these steps, and the functions are called in sequence in the specific steps step by step.

        ( In short, process-oriented emphasizes functional behavior, taking functions as the smallest unit and considering how to do it. )

2) Object-oriented : It is an "object"-centered programming idea that decomposes the problem to be solved into individual objects. The purpose of establishing an object is not to complete a step, but to describe the role of an object in solving the problem. Properties and behaviors in the steps.

( In short, object-oriented encapsulates functions into objects, emphasizes objects with functions, and uses classes/objects as the smallest unit to consider who will do it. )

     2. Comparison of advantages and disadvantages

1) Process-oriented

advantage:

Processing makes programming tasks clear, and the implementation method and final results are basically considered before development. The specific steps are clear, which facilitates node analysis.

High efficiency, process-oriented, emphasizing the short and concise code, and good at combining data structures to develop efficient programs.

shortcoming:

It requires in-depth thinking, consumes energy, has low code reusability, poor scalability, and is relatively difficult to maintain in the future.

2) Object-oriented

advantage:

The structure is clear, and the program is modular and structured, more in line with human thinking;

Easy to expand, high code reuse rate, inheritable, overridable, and low-coupling systems can be designed;

Easy maintenance and low coupling of the system help reduce the later maintenance workload of the program.

shortcoming:

The overhead is high. When you want to modify the inside of an object, the properties of the object do not allow direct external access, so you need to add a lot of behaviors that have no other meaning and are only responsible for reading or writing. This adds burden to the programming effort, increases operational overhead, and makes the program bloated.

The performance is low. Because it is oriented to a higher logical abstraction layer, object-oriented has to make sacrifices in performance when implementing it. The computing time and space storage size are very expensive.

2. Code example display


'''
以下代码使用了面向过程的开发方式。
我们定义了两个函数,一个用于计算矩形的周长,一个用于计算矩形的面积。
然后在程序中调用这些函数来计算并输出矩形的周长和面积。
'''
# 面向过程开发

def calculate_perimeter(length, width):
   perimeter = 2 * (length + width)
   return perimeter

def calculate_area(length, width):
   area = length * width
   return area

# 调用函数计算矩形的周长和面积
length = 5
width = 3
perimeter = calculate_perimeter(length, width)
area = calculate_area(length, width)
print("周长为:", perimeter)
print("面积为:", area)

Figure 1 Process-oriented 

'''
以下代码使用了面向对象的方法进行开发。
我们定义了一个矩形类(Rectangle),其中包含了矩形的长度和宽度属性,
并且定义了两个方法用于计算矩形的周长和面积。
'''
# 面向对象方法
class Rectangle:
   def __init__(self, length, width):
       self.length = length
       self.width = width

   def calculate_perimeter(self):
       perimeter = 2 * (self.length + self.width)
       return perimeter

   def calculate_area(self):
       area = self.length * self.width
       return area

# 创建矩形对象
rect = Rectangle(5, 3)

# 调用对象的方法计算矩形的周长和面积
perimeter = rect.calculate_perimeter()
area = rect.calculate_area()
print("周长为:", perimeter)
print("面积为:", area)

Figure 2 Object-oriented 

 

3. Summary

1) In process-oriented development , program design mainly focuses on the definition and calling of functions. Functions are written according to specific operation logic and complete specific tasks by passing parameters. Data and functions are separated . Functions transfer data and return results through parameters.

2) In the object-oriented method , the design of the program mainly focuses on the creation of objects and the invocation of methods . We create a rectangle object (rect) to represent a specific rectangle instance, and then complete the corresponding calculation task by calling the object's method . The object encapsulates data (i.e., the length and width of the rectangle) and operations on the data (i.e., methods for calculating perimeter and area), and the data is accessed and manipulated through methods.

 

Guess you like

Origin blog.csdn.net/nuhao/article/details/132732648