Change your mindful thinking in your program (knowing difficult) [转]

xiaoxiao2021-03-06  119

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 realized, since it is OOP, you can't say that the code is plugged into the main process, so I have designed three classes:

A class is responsible for reading a database, taking the email address, calling the QMAIL sendmail command to send;

A class is reading the email content, MIME encodes into HTML format, plus the mail head;

A primary class is responsible for reading parameters from the command, handling the 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

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 like this:

I need a counter, this counter should have such a function, refresh the page, the number of visits will be added, and there is also a function of a counter clear 0, and of course the counter can be set to any 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 what we usually habitually habit), we know that there is a counter COUNTER, there is a must and two optional methods:

GetCount () // Take the counter value method

ResetCounter () // Counter Clear 0 Method

SetCount () // Designator to the corresponding value method

The complete definition of the COUNTER class is as follows:

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:

// Here you get the ID value from the HTTP environment

...

Counter mycounter = new counter (); // get the counter

INT currentcount = mycounter.getcount (ID); // Take count from the counter

// To output to customer browser

...

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 package SELECT operations on 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:

Junkmail

Attributes:

HEAD

Body

Address

method:

Sendmail () // Send Mail

Listallmail () // Column Email Address

Use Java to expressed:

Public class junkmail {

PRIVATE STRING HEAD;

PRIVATE STRING BODY; Private String Address;

Public JunkMain () {// default class constructor

/ / Read the message header and email from the external configuration file

THIS.HEAD = ...;

THIS.BODY = ...;

}

Public static boolean sendmail (string address) {

// Call qmail, send email

}

Public Static Collection ListLallmail () {

// Access the database, return a mail address 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 knowing that you can't think about the code, you have to design a class, an interface, you must use your familiar programming language, see if you can make it, so If you are not careful, you will go back to the old way of designing 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:

The addUser () and deleteuseuser () methods are good, that is, add records and delete 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 one record

}

Public static void deleteuser (...) {

// Database Delete one 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:

Public interface itrator {

Public Boolean Hasnext () {}

Public Object next () {}

}

The User class ListUses method return value is changed to the Iterator interface:

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:

User myuser = new user ();

Iterator itrator = myuser.listusers ();

While (item.hasnext ()) {

Iterator.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

Through several examples of the above example, the use of object-oriented thinking methods is actually a process of abstracting business logic from specific programming techniques, and this abstract process is from top, very in line with human Thinking habits, that is, the details of the problem solving, the most important aspect of the problem becomes a simple framework, concentrate on how to solve the main contradiction, and then divide the details of 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.

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

New Post(0)