Big talk JS object-oriented extension of object-oriented and process-oriented game theory (OO Vs. Process)------(meditation triggered by a simple example)

Source: Internet
Author: User

One, general overview

1, the author discusses

I began to learn from Java programming to touch oop (object-oriented programming), just beginning to use Java to write a program is very awkward ( object-oriented programming because of the introduction of classes, objects, examples and other concepts, very close to the human understanding of the world and the way of thinking of all things. For complex things, how do people know, sum up, summarize? Object-oriented programming is an effort to answer this question, and the core of the answer is two words: abstraction. Therefore, object-oriented programming is particularly suitable for processing business logic, so it is widely used in the current software development. Because we develop the software is to solve the problem, object-oriented programming in line with human "problem" cognitive way ), because I used to use C to write programs (), I appreciate the simplicity and efficiency of C, like C concise and expressive ability of rich style, In particular, the Java running slow speed, relatively lengthy code, and a very simple thing to write a lot of classes, a class call a class, the heart of the resistance is strong. I have been studying the object-oriented characteristics of Java for a long time, self-understanding, but also began to consciously use the OOP style to write programs, but still often feel do not know how to refine the class, in the face of a specific problem, will feel a lot of brain, do not know how to do, accidentally, Will go back to the original idea up. Here, a complete example of login and registration runs through the full text to illustrate the object-oriented and process-oriented approach to progressive analysis and continuous refactoring to achieve our desired results.

2, Simple summary

Process-oriented is to analyze the steps required to solve the problem, and then use the function to implement these steps step by one, using a one-time call to be able.

Object-oriented is the decomposition of a problem transaction into individual objects, the purpose of which is not to complete a single step, but to describe the behavior of something in the whole process of solving the problem.

Object-oriented is the abstraction of physical height.

Process-oriented is a top-down programming.

Second, the introduction of the case

(1) Demand analysis

A, only registered users can log in to the system. Login system is very simple, only need to provide login user name and password to log in the system.

B, unregistered users can register a user name, and provide a password and simple personal information, such as e-mail address, can register successfully. It is important to note that the user name cannot be duplicated. a successful registered user can log in to the system according to the username and password you just registered.

C, the registration function of the input data items include user name, password, e-mail.

(2) example explanation, gradual

Here is a reference to the login example, which I will initially implement:

1 functionLogin (username,userpass) {2     if(!username | |!Userpass) {3         Throw NewError ("User name or password cannot be empty!")) ;4     }5 $.ajax ({6URL: "/login.json" ,7Type: "POST" ,8 data:{9 Username:username,Ten Userpass:userpass One         } , ADataType: "JSON" -}). Done (function(data) { -Console.log ("Login Successful!") ; the     }) ; -} ;

It is not difficult to see that this is a process-oriented implementation, but there is still a design phase of the wrong thinking, login its function is the role of login, but in addition to the user information validation work, obviously to separate responsibilities, continue to see the code

1 functionLogin (username,userpass) {2     if(Validate (Username,userpass)) {3 $.ajax ({4URL: "/login.json" ,5Type: "POST" ,6 data:{7 Username:username,8 Userpass:userpass9             } ,TenDataType: "JSON" One}). Done (function(data) { AConsole.log ("Login Successful!") ; -         }) ; -     } the     Else{ -         Throw NewError ("User name or password cannot be empty!")) ; -     } - } ; + functionValidate (Username,userpass) { -     return(!username | |!userpass)?false:true ; +} ;

This looks much better, at least the design is not a problem, but if the further abstraction is better, see the final code

1 functionLogin (username,userpass) {2     if(Validate (Username,userpass)) {3 Loginservice ({4 Username:username,5 Userpass:userpass6},function(data) {7Console.log ("Login Successful!") ;8         }) ;9     }Ten     Else{ One         Throw NewError ("User name or password cannot be empty!")) ; A     } - } ; - functionLoginservice (params,callback) { the $.ajax ({ -URL: "/login.json" , -Type: "POST" , - data:{ + UserName:params.userName, - UserPass:params.userPass +         } , ADataType: "JSON" at}). Done (function(data) { - callback (data); -     }) ; - } ; - functionValidate (Username,userpass) { -     return(!username | |!userpass)?false:true ; in} ;

The gradual reconstruction is much better than the original, but it is still the process of thinking, from the perspective of the machine to the real world to analyze the problem. So at the time of design, we have taken into account the details of programming implementation, and try to achieve the goal of satisfying the real-world software requirements from the starting point of implementing the program at the bottom. This method of analysis is not applicable to the object-oriented programming language such as Java, because, if you use C language, the encapsulation of two C functions, will be more than Java implementation of a lot easier, more logically clear. I think the essence of object-oriented is that thinking about the problem is based on the human thinking habits of the real world, as long as the understanding of this point, we understand the object-oriented thinking method.


Okay, we're going to use OO to implement that login example.

1, establish the user entity model class

1 functionUser (username,userpass,repass,usermail) {2      This. UserName =UserName;3      This. Userpass =Userpass;4      This. Repass =Repass;5      This. Usermail =Usermail;6 } ;7User.prototype = {8     //Set Get9} ;

2, establishing the operations API in the user entity model

1 functionUser (username,userpass,repass,usermail) {2      This. UserName =UserName;3      This. Userpass =Userpass;4      This. Repass =Repass;5      This. Usermail =Usermail;6 } ;7User.prototype = {8     //Set Get9 } ;TenUser.getuserbynameandpwd =function(params) { One     return$.ajax ({ AURL: "/login.json" , -Type: "POST" , - data:{ the UserName:params.userName, - UserPass:params.userPass -         } , -DataType: "JSON" +     }) ; -} ;

3, Establish Usermanager class

1 functionUsermanager (user) {2      This. user =user;3 } ;4Usermanager.prototype = {5Login:function(){6         if( This. Validate ()) {7 Loginservice ({8UserName: This. User.username,9Userpass: This. User.userpassTen},function(data) { OneConsole.log ("Login Successful!") ; A             }) ; -         } -         Else{ the             Throw NewError ("User name or password cannot be empty!")) ; -         } -     } , -Validate:function(){ +         return(! This. user.username | | ! This. user.userpass)?false:true ; -     } +} ;

4, establish the client test method

1 function loginclient () {2     var New Usermanager (new User ("BB", "123456")); 3     Usermgr.login (); 4 };    

5, basically this is the complete refactoring version of OO, the following is a summary

Through the above example of the design, the use of object-oriented thinking method, is actually a business logic from the specific programming technology abstracted from the process, and this abstract process is top-down, very consistent with human thinking habits, that is, not to consider the details of the problem-solving, Abstract the most important aspects of the problem into a simple framework, focus on how to solve the main contradiction, and then in the process of solving the problem, then the details of the problem into a small problem, and then specifically to solve the details of the problem.

So once this is firmly grasped, you will find that in the software design and development process, you will always unknowingly use object-oriented thinking method to design and write programs, and the design and development of the program is not so boring, and a reasonable use of object-oriented technology design and architecture software, But also has the artistic beauty of thinking.

Finally, the object-oriented way of thinking can also give you the design of the road to create fun.

Three, the final summary of

1. The duties of the class can not be many, the responsibilities need to decompose

2. Abstract Entity Model

3. Understanding the nature of OO encapsulation, inheritance, polymorphism

hahaha, the end of this article, not to be continued, hope and we have enough communication, common progress (*^__^*) hehe ...

Big Talk JS object-oriented extension of object-oriented and process-oriented game theory (OO Vs process)------(meditation triggered by a simple instance)

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.