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 to get accustomed to using C Write a program, appreciate C's simpleness and efficiency, like C concise and express abundant style, especially to endure Java running slowly, relatively long code, and a very simple thing, write a lot Class, a class calling a class, and the conflict in my 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 http: //hostname/count.cgi? ID = xxx background has a database table, Save each ID (an ID corresponding to a page of the statistical access), the request page, the field of the corresponding ID, add 1 (here we ignore the problem with the database table, the problem that appears). 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: getCount () // Take a counter value method resetCounter () // Counter Clear 0 Method SetCount () // Designing the number of the counter to the corresponding value to define the complete definition of the Counter class as follows: public class counter {public int getCount (int id) {} public void resetcounter INT ID) {} public void setcount (int id, int currentcount) {}} Solving the framework of the problem is already, let's take a look at how to use Counter. In count.cgi, call Counter to count, the program piece is as follows: // Here is the ID value from the HTTP environment ... Counter mycounter = new counter (); // get counter int currentcount = mycounter.getcount (ID); / / From the counter count / / here to output to the customer browser ... The framework of the program is all written, and the rest is to implement the specific code in the Counter class method. At this time, consider the specific programming language implementation. Details, for example, access the database in the getCount () method, update the count value. 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 it should be designed as follows: Class JunkMail properties: headbodyaddress Method: sendMail () // transmit message listAllMail () // e-mail address indicated by the column Java: public class JunkMail {private String head; private String body; private String address; public JunkMain () {// Default class constructor // From the external configuration file read the email head and email body this.Head = ...; this.body = ...;} public static boolean sendmail (String Address) {// Call qmail, send email} public static collection listallmail () {// Access database, return a mail address collection}} When the Junkmail is designed, then call the Junkmail class to complete the email, will be a very easy thing. 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.
Public class user {public static void adduser (...) {// Database INSERT A record} public static void deleteuser (...) {// database delete one record} public vector listusers (...) {// database Select results in a collection}} This 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 item, define two methods in this interface. NEXT and HASNEXT: PUBLIC Interface IteT () {} PUBLIC OBJECT NEXT () {}}}}}}} The list of listUses method returns to the Iterator interface Implementation Class: Public Class User {... public itrator Listusers () {} ...} This is separated by the design and specific implementation methods of the User class, because any new () and HasNext () are achieved at this time. The method of the method can be used as a return value of Listusers, which can be used to express the "user list", not just a 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, use the listUsers method of the User class: user myuser = new user (); item itrator = myuser.listusers (); while (item.hasnext ()) {item.next ();} This You can do it without considering the program code. From the high level, the function is abstract, defined as an interface, and it can be designed and completely based on 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.