What is the difference between object-oriented and process-oriented?

What is the difference between object-oriented and process-oriented?

This topic is actually very complicated. I think we should talk about it from the history of programming.

  1. Process-oriented programming

When the science of software development was in its very simple early days, we programmed it like this:

Define function
    Function aa
    Function bb
...
Define the data
   Data aa
   Data bb
...
then
  Pass data to the function
  Execute the function according to the specified steps
  Output result

You can basically think of the above as process-oriented programming

Process-oriented is to analyze the steps needed to solve the problem, and then use functions to implement these steps step by step, and then call the functions one by one.

As computer software applications become more and more powerful, the functions of the software become more and more powerful, with more and more lines of code, and when the complexity far exceeds Hello World, we will have trouble if we write programs/software in the above form Up.

Functions and data will be defined very much, which will cause two problems:

  • The first is a naming conflict. There are only a few English words, and there may be no suitable short words when the name is written. In order to avoid conflicts, the function name can only be made longer and longer.

  • Then there is code duplication. For example, if you make a calculator program, your function must ensure that it handles reasonable data, so that at least in the four functions of addition, subtraction, multiplication, and division, you must write the code to detect the parameters, write four times or copy and paste Four times will not be very annoying, but you will be painful if you have more, and because these detection codes are irrelevant to the original intention of your addition, subtraction, multiplication, and division functions, they must be written there, making the code bloated and intentionally ambiguous. , The intention cannot be seen intuitively.

Even the author of an online novel, every time he writes a new chapter, he is unlikely to directly open a document containing the text of the previous hundreds of chapters and continue writing. It is more normal to create a new document and write a new chapter, in order to reduce complexity and reduce interference. What's more, the code is so complicated and error-prone.

  1. Object-oriented programming

1). Preliminary solution

With the development of the software industry, solutions are coming:

  • For code duplication, we can use the method of calling functions in the function, such as extracting the detection code into an independent function, and then adding, subtracting, multiplying and dividing the four functions when running, just call the detection function to check the parameters. The repetitive code distributed in the four functions becomes one function, is it much easier to maintain?

  • Name conflicts, let's classify this bunch of functions. For example, when there is no classification, we can only choose a name:

Detect
Integer plus
Integer minus
Integer multiplication
Integer division
Plural plus
Complex number minus
Complex number multiplication
Plural division
Decimal plus
...

Is it right? A concept called class is ready to come out, so we open an integer class code file, which contains four simple addition, subtraction, multiplication and division functions (also called methods), simple and clear, but not There is a naming conflict with other addition, subtraction, multiplication, and division functions outside (because of the namespace).

Of course, after such classification, there will be new problems and solutions. For example, the detection in the four classes should also be extracted, so the simple cause eventually develops a very complicated set of programming paradigms such as encapsulation, inheritance, and polymorphism. Then some academic gods in the programming world came up with various tall names, the so-called object-oriented programming (OOP), and then went to "poison" young children like you and me.

  • Object-oriented is to decompose the elements that constitute a problem into various objects. The purpose of establishing objects is not to complete each step, but to describe the behavior of a certain element in the entire problem-solving step.

2). Further, solve the problem

After the above classification, the code is actually not easy to maintain, and then we continue to extract it as:

Number {
Detect
plus
Less
Multiply
except
}
Integer {
Follow the design from above
}
Decimal {
Follow the design from above
}

The so-called inheritance is the overall design of the number class. Integers, fractions and decimals are used as their writing outline to write the specific code of the functions of addition, subtraction, multiplication and division. According to the nature of integers, fractions and decimals, make their own adjustments. At this time, if you write some very rough and simple code for the addition, subtraction, multiplication, and division functions of the counting class, it is called the parent class, the base (foundation) class. The subclasses "inherit" the parent class (complicating the code). If the specific function implementation logic is not written in the class, then the class is actually just a design drawing like an empty shell, called an abstract class. The subclasses "implement" the abstract class (turning the empty design into concrete code). What is a template? Languages ​​like C++ are strongly typed, which differentiates the types of variables, such as integer types and double integer types. Obviously, the data volume that these two variables can hold is different. A single function cannot take multiple types of parameters. We may face the coexistence of the following two sets of codes.

Single integer class {
Single integer plus
Single integer minus
Single integer multiplication
Single integer division
}
Double integer class {
Double integer addition
Double integer subtraction
Double integer multiplication
Double integer division
}

So C++ and other strongly typed languages ​​such as JAVA provide us with a so-called template function, also called generic:

<variable type> integer {
<variable type> plus
<variable type> minus
<variable type> multiply
<variable type> except
}

Integer type is equal to setting the variable type to integer and putting on the template. Double integer type is equal to setting the variable type to double integer and putting on the template. In this way, one piece of code is written, and two pieces of code are obtained. Of course, weakly typed programming languages ​​such as my favorite JavaScript, because there is no strict type distinction for variables, there is no such annoyance. But the variable type is still very important sometimes. In weakly typed languages, operations like numbers and strings will appear, which may not be the programmer's expectation and intention. Therefore, compared with strongly typed languages, beginners are using In weakly typed languages, there are often many "boring and boring" bugs.

3). A few more verbose sentences

After the parent class is developed above, we find that programming is still problematic. The decimal class:

Decimals {
plus
Less
Multiply
except
}

If we need a decimal calculation class that "can automatically implement the result rounding function", and at the same time we need one without this function, what should we do? Should we write two classes? It's not necessary. Therefore, the geniuses of the programming world came up with the concept of "instance" or "object", and first changed the class to:

Decimals {
Identification variable: whether to round
Identification variable: whether to limit the number of digits after the decimal point
Constructor (set the above flag)
Plus (different results will be output according to the above two identification variables)
Minus (different results will be output according to the above two identification variables)
Multiply (different results will be output according to the above two identification variables)
Divide (different results will be output according to the above two identification variables)
}

In this way, we write a class, but through the constructor, a piece of code, through the keyword new to construct two instances of slightly different behavior for us to use, this process is called instantiation. Classes that cannot be instantiated are called static classes, and the behavior of functions is fixed. The classes that cannot be instantiated are actually just a collection of functions. It is just a sort of function. The powerful functions and the flexibility of coding are not enough. Classes can be instantiated and changed into instances with different behaviors. We generally call them classes because they are the most common. Programmers can also keep the code simple and at the same time can easily fine-tune code behavior.

summary

At the end of the article, I will use Gomoku as an example. The process-oriented design idea is the first step to analyze the problem: 1. Start the game, 2. His will go first, 3. Draw the picture, 4. Judging win or loss, 5. It is the white child’s turn, 6. Draw the picture, 7. Judging win or lose, 8, Return to step 2, 9 and output the final result. Implement each of the above steps with separate functions, and the problem is solved.

Object-oriented design is to solve the problem from another way of thinking. The whole gobang can be divided into 1. Black and white, the behavior of the two parties is exactly the same, 2. The board system, responsible for drawing the picture, 3. The rule system, responsible for judging fouls, winning or losing, etc. The first type of object (player object) is responsible for receiving user input and notifying the second type of object (checkerboard object) about the change in the chess piece layout. The chessboard object is responsible for displaying the change on the screen after receiving the change of the chess piece, and at the same time using the first three types of objects (rule system) are used to determine the game.

I've talked a lot about it, but I don't know if it means. It's late at night, I want to take a bath and then rest. The last sentence: Even if a new concept of "XX-oriented programming" comes out one day, we don’t have to be confused about how to come up with something new. It must have appeared to solve existing problems; it helps to think more about the reasons behind it. For us to have a deeper understanding of the programming paradigm currently used.