Object-oriented thinking Author: Fankai E-mail: robbin_fan@yahoo.com.cn I learned from the Java programming came into contact with OOP (Object Oriented Programming), started using the Java programming when feeling very uncomfortable, because I had Write a program with C to write a program, appreciate C's simplicity and efficiency, like C to express a rich style, especially the speed of Java runs slowly, relatively long code, and a very simple Things, you have to write a variety of classes, a class calling a class, and the arrogance of your heart is very strong. I have a long time for Java's object-oriented features, and I think that I have a consciousness, and I also have a conscious use of OOP style to write procedures, but I still often feel that I don't know how to refine, when I face a specific problem, I will I feel that thousands of Wanxu in my mind, I don't know how to start, I'm not careful, will return to the original idea. For example, you have to send an advertisement message, the advertisement message list exists in the database. If you use C to write, you will generally think so, first read the content content, then connect the database, loop the email address, call the version of the QMAIL command to send. Then consider using Java to be implemented, since it is OOP, if you can't put it into the main process, then you have designed three classes: a class is responsible for reading the database, taking the email address, calling qmail sendmail commands; one Class is read mail content, MIME encodes into HTML format, plus mail head; a primary class is responsible for reading parameters from command, processing command line parameters, calling the Email class. Putting a piece of work to three modules to separate, each class completes a module task. Careful analysis, it will find that such a design is completely designed from the perspective of programmers, or when designing, it is from the perspective of the machine from the perspective of the machine to the real world. questionable. Therefore, when designing, the details of programming implementation have been taken in, trying to achieve the starting point of the underlying implementation program to meet the demand for software requirements in the real world. Such an analytical method is actually not suitable for object-oriented programming languages such as Java, because if you switch to C language, pack two C functions, it will be easier than Java, logically clearly. I think the object-oriented essence is to consider the problem of the problem of human thinking from the real world. As long as I appreciate this, I will understand the object-oriented thinking method. To give a very simple example: If you need to write a web counter, the customer accesses the page, the web counter adds 1, the counter is accessible
Code: http://hostname/count.cgi? Id = xxx
There is a database table in the background, saving each ID (a ID corresponding to a page of the statistical access), the request page, the field of the corresponding ID, the field of the counter plus 1 (here we ignore the concurrent update database table, appear Table lock problem). If we will be analyzed in accordance with the general point of the program, we will consider: first, take the ID from the HTTP GET request, then follow the ID database table, get the access count value corresponding to a ID, then add 1, update the database, and finally Display the access count to the page. Now suppose a person without programming experience, how will he think about this problem? What kind of need he will mention? He is likely to think so: I need to have a counter, this counter should have such a function, refresh the page, the access amount will add 1, and the other is also the function of the counter clear 0, of course, if there is a counter If you are a function of arbitrary value, I can cheat. As a person without programming experience, he does not think about how to operate on the database, how to pass on the HTTP variable, what is the problem of the problem, what is my business logic, the software should What is the function. According to this idea (please note that his idea is actually the way we usually habits), we know that there is a counter COUNTER, there is a must and two optional methods: code: getcount () // Take the counter value method resetCounter () // counter Clear 0 method setCount () // Designor to the corresponding values
The complete definition of the COUNTER class is as follows:
Code: Public Class Counter {Public INT getCount (int ID) {} public void resetCounter (int id) {} public void setcount (int id, int currentcount) {}}}
The framework for solving the problem is already, come and see how to use Counter. Call the Counter in count.cgi, the program is as follows:
Code: // Here you get the ID value from the http environment ... Counter mycounter = new counter (); // get counter int currentcount = mycounter.getcount (ID); // Note from the counter //> View to customers here Output ...
The framework of the program is written, and the rest is to implement the specific code in the Counter class method. At this time, consider the details of the specific programming language implementation, for example, access the database, update the count value in the getCount () method. As seen from the above example, the object-oriented thinking is actually the way we habits in real life. It is a way of translating the way of solving problems in the perspective of human considering problems. The process, in the process of this translation, the software is gradually designed. In the process of software design using object-oriented thinking, the easiest error is to start analysis, think of the details of the program code implementation, so the package is based entirely on the program implementation logic, not solving problems. Business logic. The classic error of learning JDBC programming is: "How do I encapsulate SELECT operations for the database?" Object-oriented design is based on the design of business problems, not based on specific programming technology. I won't pack the SELECT statement, I only package the problem of business logic to solve the problem, and the reading of the database is the problem of the business logic's encoding implementation stage. Going back to see the example of the advertisement email, how should I apply object-oriented thinking? For a message, there is a mail header, a mail body, and a mail address, send mail, you need a method of sending, and there is another way to list all email addresses. So you should design: Code: Class JUNKMAIL Property: Head Body Address method: sendmail () // Send mail listAllmail () // Rouge email address to reply to: public class junkmail {private string head; private string body; private string Address; Public JunkMain () {// Default class constructor // From the external configuration file read mail head and email body this.Head = ...; this.body = ...;} public static boolean sendmail ) {// call qmail, send email} public static collection listallmail () {// Access Database, return a message collection}}}
When the Junkmail is designed, then call the Junkmail class to complete the sender of the message, it will be very easy. If the traditional process-oriented programming is the process that meets the process of machine running instructions, the object-oriented thinking is to meet the thinking process of human solution in real life. When the object-oriented software analysis and design, to remind himself, don't go up to the implementation of the program code, you should throw the constraints of specific programming languages, focus on analyzing the business logic of the software we want to implement, analyze software Business processes, thinking about how to describe and implement software business. After all, the software is just a carrier, and the business is the goal we really want to achieve. But in the design process, my heart is often worried. If I don't think about the implementation of the program code, then how do I know that my design is reasonable? How do I know the class I design, can the interface be implemented? So often you can see is: In the design process, although you know that you can't think of your code implementation, every design, an interface, you must use your familiar programming language probably, See if you can make it out, so, you will go back to the old road that is designed according to the idea of the program function. For example, you will often encounter a page display data when doing a web programming. For example, it is necessary to list all users in the system. Assume that users use the User class to indicate users, increase user adduser (), delete user deleteuser (), query all user listUsers () methods. There is a USER table in the database, and a record is information about a user. Let's take into account the implementation of the User class: adduser () and deleteuser () methods are good, that is, add recording and deletion records for the database. For the listusers () method, it is actually a SELECT for the USER table, takes out a recordset. But how do you get a list of all users from a listUsers () method? The return value called by a method is only one, no multiple, so many ways to use is that the return value is defined as a collection type, such as Vector. This will be implemented when the specific code of the listUsers () method is implemented from the database, and insert it into the vector. In the main program, call the listUsers () method to return a vector, and then operate to the VECTOR traversal, you can get a list of users. Code: Public class user {public static void adduser (...) {// Database INSERT A record} public static void deleteuser (...) {// Database delete a record} public vector listusers (...) {/ / Database SELECT results are put in a collection}}
Such a design is basically reasonable, but still a little problem. Because in design, considering the storage of unordered length data sets, so that the principle of object-oriented design is violated: the implementation of specific program language should not be considered prematurely. . Therefore, it is necessary to express business logic with abstract methods, and specific implementation-independent methods. We know that it is usually traversed for data structures with aggregate features, which can usually use next and HasNext methods. Next implementation takes off a user, HasNext determines if there is an element. So we define an interface Iterator, define two methods in this interface, NEXT and HASNEXT:
Code: Public interface itrator {public interface iterator {public boolean hasnext () {} public object next () {}} ListUses method returns to the Iterator interface:
Code: public class user {... public itrator listusers () {} ...}
This is separated by dividing the User class's design and specific implementation method, because any class that implements the next () and HASNext () methods can be used as the return value of ListUsers, can be used to express "user list ", But not only you can use Vector. For example, I can use arraylist to express the user list because ArrayList also implements iterator. Of course, I can also write a class to store a list of users, as long as the next () and HasNext () methods will be used. Thus, when the specific writing code, the programmer has the greatest flexibility, and can use different programming methods to store the user list according to the specific case. In particular, the coupling of the program is reduced, which improves the portability of the program. For the listedmail () method of the JUnkmail above, it should also be changed to the interface type. Then, in the main program, this is used to use the User class ListUsers method:
Code: user myuser = new user (); iterator itrator = myuser.listusers (); while (item.hasnext ()) {item.next ();}
This will not consider the program code, and the function is abstracted from the high level, defined to become an interface, and the system design can be designed and fully designed according to the needs of the business. Conclusion The design instructions of the above examples use the object-oriented thinking method, in fact, a process of abstracting business logic from specific programming techniques, and this abstract process is from top, very in line with humanity The thinking habit, that is, the details of the problem solving first, the most important aspect of the problem becomes a simple framework, concentrate on how to solve the main contradiction, and then divide the problem in the process of solving the problem A small problem, then dedicate the details of the details. Therefore, once it firmly seized this, you will find that you will always use object-oriented thinking methods to design and write programs in the software design and development process, and write programs, and program design and development. It has also become so boring, and a reasonable software for designing and architecture of object-oriented technology has the artistic beauty of thinking. Finally, I am willing to object-oriented thinking methods can also bring your program to create fun.