Path to Architect (2)-detailed description for process-oriented Wang zebin

Source: Internet
Author: User

 

2.3 relationship between process-oriented programming (OPP) and Object-Oriented Programming (OOP)

Many people have defined process-oriented programming (OPP) and Object-Oriented Programming (OOP). You can find a very professional explanation from any materials, however, in my experience, it is relatively boring and not intuitive. Unless you have already accumulated a considerable amount of data, it is still difficult to say.

I am an old manProgramAlthough my daily work tends to be more manageable, I still keep coding habits. What does this mean? I should be able to communicate with you. Whether you are repeating the path I 've traveled, or you are already ahead of me, everyone will have the same experience and have a kind of understanding and tacit understanding at the ideological level, so I will try my best to write it down according to the conventional thinking of most people.

Process-oriented programming (OPP) comes first, and Object-Oriented Programming (OOP) is born, so Object-Oriented Programming (OOP) will certainly inherit some of the advantages of the former, and discard some shortcomings of the former, which is in line with the natural law of human progress. In their respective development and evolution, the two will also learn from each other and integrate with each other to absorb the advantages of the other party, so as to show the convergence in some aspects. These are the inevitable results. Even if the two have more similarities, they will not change their differences in nature, because their starting point is completely two completely different ways of thinking. In my opinion, the relationship between the two is as follows: Object-oriented Programming (OOP) must be process-oriented (OP) and process-oriented programming (OPP) the idea of object-oriented (OO) should be used for reference as a whole. This paragraph is indeed very empty, and it will certainly lead to controversy. However, I advise you to judge whether my point of view is correct after reading the content below.

For example, C ++, C #, and Java are all object-oriented languages. C and PHP (for the moment, since PHP 4 will support oo later) are all process-oriented languages, so is it true that the program I write with C ++ must be object-oriented, and the program I write with C must be process-oriented? This is clearly not the difference between the two. Language is always a tool. Every language created by the predecessors is a tool you use to realize your ideas. I think many people use C # and Java to writeCodeIf you take a closer look, it is actually a process-oriented (OP) program written in object-oriented (OO) language.

So, even if you give Guan Yu a wooden stick and give you a knife, he can beat you full of bags. You just carry a treasure knife, and you cannot become Guan Yu, because you lack the most essential thing-the peerless martial arts. In the same way, if you do not understand the OO idea, how can you write a real OO program? Object-oriented (OO) and process-oriented (OP) are two completely different ways of thinking.

Is it a process-oriented issue and there is no need for it? I have never said so. As a matter of fact, process-oriented programming (OPP) has existed for decades, and many people are still using it. Its advantage is that it is easy to understand when the logic is not complex, and its running efficiency is much higher than that of object-oriented programs. Therefore, process-oriented programming (OPP) is still used in system-level applications or quasi-real-time systems ). Of course, many programmers and masters like to use process-oriented programming (OPP), such as Apache, Qmail, and Postfix, because they have strong control over the entire system, ice and so on. These classic systems are the product of OPP.

Script languages like PHP are mainly used for web development. For some systems with relatively simple business logic, process-oriented programming (OPP) is often used ), this is one of the reasons why PHP cannot enter enterprise-level application development. However, PhP5 currently supports oo well.

 

2.4 process-oriented programming (OPP)
 

Before the emergence of object-oriented, we used process-oriented programming (OPP ). One of the most common analysis methods for process-oriented programming is "functional decomposition ". We will first break down user requirements into modules, then break down modules into major functions, and then break down large functions into small functions. The whole requirement is in this way, finally, it is decomposed into one function. This solution is called "top-down". The principle is "first overall, then local" and "first big, then small". Some people also like to use the "bottom-up" analysis method, first solve the local difficulties, gradually expand, and finally combine the entire program. In fact, these two methods can solve the problem in the end, but it is still common to use the "top-down" method, this method is the easiest way to see the nature of the problem.

Let me give an example to illustrate the process-oriented programming method:

User requirements: The boss asked me to write a general calculator.

The end user is the boss. As a programmer, the task is to write a calculator program. OK. The following is a calculator completed in C language:

Assume that the program file name is main. C.

Int main (INT argc, char * argv []) {

// Variable Initialization
Int nnum1, nnum2;
Char copr;
Int nresult;
Nnum1 = nnum2 = 0;
Copr = 0;
Nresult = 0;

// Input data
Printf ("Please input the first number: \ r \ n ");
Scanf ("% d", & nnum1 );
Printf ("Please input the operator: \ r \ n ");
Scanf ("% s", & copr );
Printf ("Please input the second number: \ r \ n ");
Scanf ("% d", & nnum2 );

// Calculation Result
If (copr = '+ '){
Nresult = nnum1 + nnum2;
} Else if (copr = '-'){
Nresult = nnum1-nnum2;
} Else {
Printf ("unknown operator! ");
Return-1;
}

// Output result
Printf ("the result is % d! ", Nresult );
Return 0;
}

 

Aside from the details, I think most people will achieve this. It is very clear and simple. It fully embodies the principle that "Simplicity is beauty, process-oriented programming is such a systematic way to gradually implement user needs in order.

Anyone who has done the program knows that user requirements are never stable, and they can only achieve "relatively stable" at most ". The user may request to add a function at any time, reduce the number of functions, or change the process. The most annoying thing for programmers is to change the requirements frequently, in particular, the program has already been written for more than half, but this situation can never be avoided, and it cannot be completely blamed for customers or demand analysts.

Taking the above Code as an example, users may make similar requirements:
First, you implement "addition" and "subtraction" in your program. I also want it to calculate "multiplication" and "division ".
Secondly, your current man-machine interface is too simple. I also want a Windows Calculator interface or a Mac calculator interface.

There are more user requirements, so I have to figure out how to write this code. Today I added the multiplication, division, and tomorrow I have to add a square orCubeIf all the operations are exhausted, you have to write eight hundred lines of code. In addition, the user needs to change the interface, and has to write a lot of code generated on the interface, and get eight hundred lines. In the future, how can I maintain so many codes together? I have to find a variable for half a day and read the code for half a day. If I accidentally make a mistake, I have to adjust it for half a day. In addition, I am not good at interface design. I have to find a more professional person to do it. After that, I will try again. This process is the process of "software crisis. With the wide application of software in various fields, the scale of software development becomes larger and more complex, and the user requirements become increasingly unstable.

Based on the two requirements raised by users, how should we deal with process-oriented programming? I think everyone knows how to change it. Very easy: Separate "computing" and "interface" into two independent functions and encapsulate them into different files.
Assume that the program file name is main. C.

# Include "interface. H"
# Include "Calculate. H"
Int main (INT argc, char * argv []) {

// Variable Initialization
Int nnum1, nnum2;
Char copr;
Int nresult;
Nnum1 = nnum2 = 0;
Copr = 0;
Nresult = 0;

// Input data
If (getparameters (& nnum1, & nnum2, & copr) =-1)
Return-1;

// Calculation Result
If (calcmachine (nnum1, nnum2, copr, & nresult) =-1)
Return-1;

// Output result
Printf ("the result is % d! ", Nresult );

Return 0;
}

Interface. h:
Int getparameters (int * nnum1, int * nnum2, char * copr );

Interface. C:
Int getparameters (int * nnum1, int * nnum2, char * copr ){
Printf ("Please input the first number: \ r \ n ");
Scanf ("% d", nnum1 );
Printf ("Please input the operator: \ r \ n ");
Scanf ("% s", copr );
Printf ("Please input the second number: \ r \ n ");
Scanf ("% d", nnum2 );

Return 0;
}

Calculate. h:
Int calcmachine (INT nnum1, int nnum2, char copr, int * nresult );

Calculate. C:
Int calcmachine (INT nnum1, int nnum2, char copr, int * nresult ){
If (copr = '+ '){
* Nresult = nnum1 + nnum2;
} Else if (copr = '-'){
* Nresult = nnum1-nnum2;
} Else {
Printf ("unknown operator! ");
Return-1;
};
Return 0;
}

Process-oriented programming (OPP) refers to "functional decomposition" of user requirements ". Break down user requirements into modules (. H ,. c), and then the module (. H ,. c. Break down a large function into a small function, and so on.

Functional decomposition is a very technical task. It requires not only rich practical experience, but also scientific theories as a guide. How to break down, what is the principle of decomposition, and how appropriate the module granularity is? These are all questions that architects need to consider, and they are also what we will focus on later.

Process-oriented programming (OPP) has the advantage of sequential execution of programs and clear processes. Its disadvantage is that the Master Program undertakes too many tasks. Each module requires the master program to control and schedule tasks, and the tasks undertaken between the master and the module are not balanced.
Some people define process orientation:Algorithm+ I think the data structure is also very accurate. In process-oriented programming, algorithms are the core, data is subordinate, and data flows with algorithms. Therefore, process-oriented programming is adopted. Generally, a flowchart or data flow diagram must be compiled before you start.

 

--------------------------------------------------------

If you have any comments on this article, you are welcome to make a picture at any time. I have the same heart as steel and the same face as the city wall, and have a strong bearing capacity. Therefore, no matter whether I support or disagree with the objection, I will read it carefully. As long as it is not pornographic or reactionary remarks, I try not to delete the post. Due to the limited personal ability and time, you can only write at this level. Thank you for your understanding. My information contains the QQ number. After adding it as a friend, I can see that my QQ is open to anyone.

 

(To be continued)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.