Process Oriented vs Object Oriented

To learn programming, the basic skill is to master the programming language, but the essence of programming is logic, so the cultivation of programming thinking is also very important. Process-oriented and object-oriented are two important programming ideas. The following describes the differences and advantages and disadvantages of the two.

1. Process-oriented

Process-oriented is an event-centered programming idea. When programming, analyze the steps to solve the problem, and then use functions to realize these steps, and then call the functions in order in the specific steps step by step.

For example, when playing backgammon, the process-oriented design idea is to first analyze the steps to solve this problem:

(1) Start the game (2) Heizi goes first (3) Draw the picture (4) Determine whether to win or lose (5) It is Bai's turn (6) Draw the picture (7) Determine whether to win or lose (8) Return to step (2) (9) Output the final result.

Use functions to implement the above steps one by one, and then call the above functions in turn in the main function of playing backgammon (different programming languages ​​have different calling methods, what I write here is direct call):

下五子棋{

开始游戏();

黑子先走();

绘制画面();

判断输赢();

轮到白子();

绘制画面();

判断输赢();

返回到 黑子先走();

输出最后结果;

}

It can be seen that process-oriented always pays attention to how to judge the winning or losing of the chess game step by step, and realize the sequential execution of functions by controlling the code.

2. Object-oriented

In daily life or programming, simple problems can be solved with process-oriented thinking, which is direct and effective, but when the scale of the problem becomes larger, using process-oriented thinking is far from enough. So slowly the idea of ​​object-oriented programming emerged. There are many people and things in the world, each of which can be regarded as an object, and each object has its own attributes and behaviors, and objects interact with each other through methods. Object-oriented is a kind of "object"-centered programming idea. It decomposes the problem to be solved into various objects. The purpose of establishing an object is not to complete a step, but to describe an object in the entire problem-solving steps. properties and behavior.

In the example of playing backgammon, if the object-oriented method is used to solve it, first divide the entire backgammon game into three objects:

(1) Black and white parties, the behavior of the two parties is the same.

(2) Chessboard system, responsible for drawing the picture

(3) The rule system is responsible for judging fouls, winning and losing, etc.

Then assign some properties and behaviors to each object:

(4) The first type of object (black and white) is responsible for accepting user input and notifying the second type of object (chessboard system) of the change in chess piece layout. The chessboard system receives the change of chess pieces and is responsible for displaying the change on the screen. At the same time, the third type of object (rule system) is used to judge the game.

It can be seen that object-oriented is to divide the problem by function, not to solve it by steps. For example, the behavior of drawing a picture is scattered in multiple steps in the process-oriented process, and different drawing versions may appear, so various simplifications should be made taking into account the actual situation. In object-oriented design, drawing can only appear in the object of the chessboard system, thus ensuring the unity of drawing.

3. Comparison of advantages and disadvantages

process oriented

advantage:

The process makes the programming task clear, basically consider the implementation method and the final result before development, the specific steps are clear, and it is convenient for node analysis.

High efficiency, process-oriented, emphasizing the shortness of the code, 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 later.

object oriented

advantage:

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

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

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

shortcoming:

The overhead is high. When modifying the inside of the object, the properties of the object do not allow direct access from the outside, so it is necessary to add many behaviors that have no other meaning and are only responsible for reading or writing. This adds to the burden of programming, increases runtime overhead, and makes programs appear bloated.

Low performance. Due to the higher logical abstraction layer, when object-oriented is implemented, performance sacrifices have to be made, and the calculation time and space storage size are very expensive.

Give examples to illustrate the advantages and disadvantages of the two, such as adding the function of regretting chess in the backgammon game. In process-oriented design, the entire steps from input to display to final judgment must be changed, and even the order of function calls must be changed. In object-oriented design, it is only necessary to add a backtracking function to the chessboard system. The attributes and behaviors of both black and white and the rule system do not need to be changed. This example illustrates the low correlation between codes in object-oriented programming (low-coupling feature), which makes the code easy to be reused and expanded, and also illustrates the low reusability and poor scalability of process-oriented code.

The above content is transferred from the programming youth programming enthusiasts learning and exchange community-programming youth

Guess you like

Origin blog.csdn.net/weixin_39570751/article/details/131085577