US design mode of learning (D): object-oriented process-oriented compared to what advantage? Process-oriented really obsolete?

In addition to object-oriented addition, the well-known programming paradigms there are two other process-oriented programming and functional programming. This process-oriented programming paradigm with the emergence of object-oriented, has gradually withdraw from the stage, and functional programming has not been widely accepted.

At work, a lot of people do not know the difference between an object-oriented and process-oriented, always thought that the use of object-oriented programming languages ​​do develop, it is engaging in the object-oriented programming. In fact, they are just using the object-oriented programming language, process-oriented style of writing code only, and did not play the advantages of object-oriented programming. This is equivalent to a hand Tulong, but just treat it as an ordinary use of a sword, a pity.

What is the process-oriented programming and process-oriented programming language?

We can compare the two concepts of object-oriented programming and object-oriented programming language, to understand the process-oriented programming and process-oriented programming language. Remember how before the definition of object-oriented programming and object-oriented programming language do? Let's look back a bit.

  • Object-oriented programming is a programming paradigm or programming style. It is a generic object as tissue or a basic unit of code, and encapsulation, abstraction, inheritance, polymorphism four properties, as the cornerstone of code design and implementation.
  • Support object oriented programming language grammar classes or objects mechanism, and a ready mechanism syntax, can easily realize the four characteristics of object-oriented programming (encapsulation, abstraction, inheritance, polymorphism) programming language.

Analogy with the definition of object-oriented programming object-oriented programming language, two concepts for the process-oriented programming language and the process-oriented programming, such definitions given below.

  • Process-oriented programming is a programming paradigm or programming style. It process (may be appreciated that a method, function, operation) as a basic unit of organization code, data (member variables as will be appreciated, attributes) and phase separation of the most important features. Process is a process-oriented style of programming style, a method operates by performing a splicing sequence data to complete a set of functions.
  • First of all process-oriented programming language is a programming language. Its greatest feature does not support the concept of classes and objects two syntax does not support rich object-oriented programming features (such as inheritance, polymorphism, encapsulation), supports only process-oriented programming.

However, it must be declared here, as we mentioned before, object-oriented programming and object-oriented programming language is not the same as the official definitions, here defined process-oriented programming and process-oriented programming language I have given, it is not the official definition of strict. The reason to give such a definition, just to be with object-oriented programming and object-oriented programming language to be a contrast to facilitate your understanding of the difference between them.

Definition is not very strict, but also more abstract, so I use an example to explain further. Suppose we have a text file that records the user information users.txt, the format of each line of text is name&age&gender(for example, 小王 &28& 男). We want to write a program, the users.txtfile is read line by line the user information, and then formatted into nametagetgender(wherein, tis the separator) this text format, and in accordance with the agefollowing arrival sorting small, re-written to another text file formatted_users.txtin. For the development of such a small program, we look at a piece, process-oriented and object-oriented with two programming styles, written by code any different.

First, let's look, write out a process-oriented style of programming code for this is what it looks like. Note that the following is the code Clanguage of this process-oriented programming languages to write.

struct User {
  char name[64];
  int age;
  char gender[16];
};

struct User parse_to_user(char* text) {
  // 将text(“小王&28&男”)解析成结构体struct User
}

char* format_to_text(struct User user) {
  // 将结构体struct User格式化成文本("小王\t28\t男")
}

void sort_users_by_age(struct User users[]) {
  // 按照年龄从小到大排序users
}

void format_user_file(char* origin_file_path, char* new_file_path) {
  // open files...
  struct User users[1024]; // 假设最大1024个用户
  int count = 0;
  while(1) { // read until the file is empty
    struct User user = parse_to_user(line);
    users[count++] = user;
  }
  
  sort_users_by_age(users);
  
  for (int i = 0; i < count; ++i) {
    char* formatted_user_text = format_to_text(users[i]);
    // write to new file...
  }
  // close files...
}

int main(char** args, int argv) {
  format_user_file("/home/zheng/user.txt", "/home/zheng/formatted_users.txt");
}复制代码

Then, we will look at using this style of programming object-oriented code to write out what it was like. Note that the following code is written in Javaan object oriented programming language to write this.

public class User {
  private String name;
  private int age;
  private String gender;
  
  public User(String name, int age, String gender) {
    this.name = name;
    this.age = age;
    this.gender = gender;
  }
  
  public static User praseFrom(String userInfoText) {
    // 将text(“小王&28&男”)解析成结构体struct User
  }
  
  public String formatToText() {
    // 将结构体struct User格式化成文本("小王\t28\t男")
  }
}

public class UserFileFormatter {
  public void format(String userFile, String formattedUserFile) {
    // Open files...
    List users = new ArrayList<>();
    while (1) { // read until file is empty 
      // read from file into userText...
      User user = User.parseFrom(userText);
      users.add(user);
    }
    // sort users by age...
    for (int i = 0; i < users.size(); ++i) {
      String formattedUserText = user.formatToText();
      // write to new file...
    }
    // close files...
  }
}

public class MainApplication {
  public static void main(Sring[] args) {
    UserFileFormatter userFileFormatter = new UserFileFormatter();
    userFileFormatter.format("/home/zheng/users.txt", "/home/zheng/formatted_users.txt");
  }
}复制代码

From the above code, we can see that the process-oriented and object-oriented basic difference is that the code is organized differently. Process for the style code is organized into a set of methods and data structures set ( struct User), the definition of the methods and data structures are separated. Style object-oriented code is organized into a set of classes, methods, and data structures are bound together, in the definition of the class.

Object-oriented programming process-oriented programming compared to what advantage there?

Why object-oriented programming occurs later than process-oriented programming, but it can instead become mainstream programming paradigm? Object-oriented programming with process-oriented programming than, in the end what are the advantages?

1. more able to cope with large and complex development programOOP

For the development of simple procedures, whether it is a process-oriented programming style, or object-oriented programming style, not really a big difference, and even sometimes, process-oriented programming style actually an advantage. Because the demand is simple enough, the entire process flow of a main program only, it can easily be divided into several sequential steps, and then stepping translated into code, which is very suitable for such a process-oriented programming style spaghetti achieved.

But for the development of large and complex program, the processing flow of the whole process complicated, not only a main line. If the processing flow of the whole process drawn out, it would be a network structure. If we then process-oriented programming such processes, linear way of thinking, to translate this network structure, to think about how to program for the dismantling method for performing a set order, it will be more difficult. This time, object-oriented programming style is more obvious advantages.

Object-oriented programming is the class think object. When performing object-oriented programming, we do not think to go one up, how complex a process as a dismantling method, instead of using Quxianjiuguo strategy, thinking about how to go to the business model, how the translation needs as a class, how to build interactive relationships between classes, and complete these tasks do not need to fully consider the intricate processes. When we have the kind of design, and then like building blocks, in accordance with the processing flow, the class assembled to form the entire program. This development model, way of thinking, allows us to address the complex at the time of program development, thinking more clearly.

In addition, object-oriented programming also provides a clearer, more modular code organization. For example, we have developed a trading system business electricity, business logic complexity, a large amount of code, you may want to define hundreds of functions, hundreds of data structure, then how different categories to organize these functions and data structure in order not to look more it messy? Class manner that these functions and data structures of a very good organization, the code is an effective means of modularity.

In fact, the use of process-oriented programming language can still write code object-oriented style, although there may be an object-oriented programming language to write object-oriented style of code than with the price paid is higher. Moreover, the process-oriented programming and object-oriented programming is not entirely opposed. Many software development, despite the use of a process-oriented programming language, also have learn from some of the advantages of object-oriented programming.

2. style code easier to reuse, easy to expand, easy to maintainOOP

Process-oriented programming is a very simple programming style, it does not provide a feature-rich object-oriented programming like that. And object-oriented programming provides encapsulation, abstraction, inheritance, polymorphism of these properties, can greatly satisfy the complex programming requirements, we can write more easily reuse, easy to expand and easy to maintain code. Why do you say?

First, let's look at the package properties. Object-oriented programming package feature is a fundamental difference in comparison to the process-oriented programming, since it is based on the concept of an object oriented programming in the basic class. Object-oriented programming code organized by class of this embodiment, the data and methods bound together by the access control, the method allows only a limited access to data in the external caller by exposure class, and not process-oriented programming as above, the data any method may be freely modified. Thus, the package provides the characteristics of object-oriented programming code that is more conducive to improve the maintainability.

Secondly, we look at abstract characteristics. We know that the function itself is an abstraction that hides the implementation. When we use the function, you only need to know what functions have a function, without the need to know is how to achieve it. From this point, regardless of process-oriented programming or object-oriented programming, support the abstract properties. However, object-oriented programming also provides implementations of other abstract characteristics. These implementations are not available for the programming process, based on such an abstract interface. Based on the abstract interface, allows us to achieve without changing the original, easily replaced with a new realization of logic, improves the scalability of the code.

Again, we look at the inherited characteristics. Object-oriented programming is inheritance characteristics compared to one of two characteristics peculiar to the process-oriented programming (the other is polymorphic). If two classes have some of the same properties and methods, we can use these same codes, drawn to the parent class, so that the two sub-class inherits the parent class. So that the two sub-classes also can reuse the code in the parent class, to avoid duplication of code written many times, enhance code reusability.

Finally, we look at multi-state characteristics. Based on this characteristic, we need to modify a function of time to achieve, you can achieve a new way subclass, override the original function logic in a subclass, replace the parent class with subclasses. In the actual process of running code, call the new sub-class of functional logic, rather than make changes in the original code. This is to comply with the "closed for modification, to expand open" design principle, improve the scalability of the code. In addition, use of multi-state characteristics of different classes of objects can be passed to the same method, different code execution logic, improving the reusability of code.

So, based on these four characteristics, the use of object-oriented programming, we can more easily reuse easy to write, easy to expand, easy to maintain code. Of course, we can not say that the use of process-oriented style will not be able to write easy to reuse, easy to expand, easy to maintain code, but without the help of the four properties, the costs may have to be higher.

3. The language is more humane, more advanced, more intelligentOOP

Initially dealing with human machine is through 0, 1this binary instructions and assembly language, and then only after the emergence of high-level programming language. In high-level programming language, a process-oriented programming language and appear earlier than object-oriented programming language. The reason comes first process-oriented programming language, it is because of the interaction with the machine the way, from the binary instructions, assembly language to process-oriented programming language, is a very natural transition, is a process-oriented, spaghetti programming style , a set of instructions with the operation sequence data to complete a task.

Increasingly easy to compile from the command and then to process-oriented programming language, way of dealing with the machine constantly evolved, from which we can easily find such a law, and that is more user-friendly programming language, people dealing with machine . General point of speaking, is more advanced programming language. In fact, after the process-oriented programming language, an object-oriented programming language appear, this development also conforms to the law, that is, object-oriented programming language is more advanced than the procedural language!

With binary instructions, assembly language, compared to the process-oriented programming language, object-oriented programming language programming routines, way of thinking, is completely different. The first three is a computer way of thinking, and the object is a human-oriented way of thinking. We, we are thinking in front with three languages ​​programming, how to design a set of instructions that tell the machine to perform this set of instructions, operating some data to help us complete a task. While performing object-oriented programming, we are thinking about how to business modeling, how the real world map as a class or an object, which makes us more able to focus the business itself, rather than thinking about how to deal with the machine. So to speak, the more high-level programming languages ​​from machine "farther" away from us human beings "closer", more "intelligent."

Just follow the law of development of the programming language used to think that if a new breakthrough programming language appear, that it is certainly more "intelligent". Bold Imagine using this programming language, we do not need to have any knowledge of computer knowledge, as they are now without having to knock on a lot of code line by line, just need to be clearly written requirements document and to automatically generate software we want.

RECAP

1. What process-oriented programming? What process-oriented programming language?

In fact, the process-oriented programming and process-oriented programming language and there is no strict official definition. The best way to understand these two concepts are compared with object-oriented programming and object-oriented programming language. Compared to the basic unit of object oriented programming class for the organization code, the process-oriented programming is a process (or method) as a basic unit of organization code. It is the most important feature data and phase separation. Compared to object-oriented programming language, process-oriented programming language, the biggest feature is not supported by a rich object-oriented programming features, such as inheritance, polymorphism, encapsulation.

2. What are the advantages compared to object-oriented programming-oriented procedural programming?

  • There are three main advantages compared from process-oriented programming object-oriented programming.
    For the development of large and complex program, the processing flow of the program is not a single main line, but the intricate network structure. Object-oriented programming than process-oriented programming to better deal with this type of complex program development.
  • Object-oriented programming process compared oriented programming, a more feature-rich (encapsulation, abstraction, inheritance, polymorphism). Use code written out of these features, more scalable, and easy re-use, easy to maintain.
  • From the evolution of the law of the programming language to deal with the machine the way, we can conclude: object-oriented programming language than process-oriented programming language, more humane, more advanced, more intelligent.

Think

Object-oriented programming than process-oriented programming, much easier to deal with the development of large and complex program. But like Unix, Linuxthese complex systems, are also based Cprogramming language, the language of this process-oriented development, how do you view this phenomenon?

Reference: Compared What are the advantages of object-oriented process-oriented there? Process-oriented really obsolete?

This article from the blog article multiple platforms OpenWrite release!

For more information, please click on my blog Mu Chen

Guess you like

Origin juejin.im/post/5de11d47f265da05ef59fe89