[Oo] object-oriented thinking method

zhaozj2021-02-16  155

Author: Fankai E-mail: robbin_fan@yahoo.com.cn

I started to contact OOP (object-oriented programming) from learning Java programming, I feel very awkward when I start using Java write programs, because I have used it to write a program with C, I appreciate the simpleness and efficiency of C, like C Constant, the ability to express the ability, especially the speed of Java running slowly, relatively long code, and a very simple thing, to write a variety of classes, a class calling a class, the arrogance 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 you should design: Class Junkmail Attribute: Head Body Address method: sendmail () // Send a mail listAllmail () // The email address is expressed in 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 the database, return a mail address collection}} When Junkmail is designed, then call the Junkmail class to complete the email transmission, 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? (Is a problem: (), the phenomenon that can often be seen is: In the design process, although I know that I can't think of the code is realized, I have a class, an interface, my heart, I don't know how to be familiar with themselves. The language is probably evaluated, see if it can be compiled, so it will go back to the old road to design the idea of ​​the program function. For example, when doing the web programming It is often necessary to encounter a page display data. For example, you need to list all the users in the system. Suppose you use the User class to represent users, add user addUser (), delete user deleteuser (), and query all users Listusers () method. There is a user table in the database. A record is a user's information. Let's consider the implementation of the User class method: addUser () and deleteuser () methods are good, that is, add record and deletion on the database. Record. For the listUsers () method, it is a SELECT for the USER table, which takes a recordset. But how do you get a list of all users from a listUsers () method? One method called the return value only one, no multiple, So many cases use the way is that the return value is defined as a collection type, such as a vector. This will take a record from the database in order to the vector in the database, inserted into the vector. In the main program Inside, call the listUsers () method to return a vector, and then operate with 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.

转载请注明原文地址:https://www.9cbs.com/read-8874.html

New Post(0)