Object-oriented thinking
Author: Fankai
E-mail:
Robbin_fan@yahoo.com.cn
I am from learning Java programming to contact OOP (object-oriented programming)
I feel very awkward when I started using Java writing programs, because I am used to it.
Use C to write a program, appreciate C's simpleness and efficiency, like C concise and express
Rich style, especially tolerate Java running slowly
, Relatively lengthy code, and a very simple thing, write a number of classes
A class calls a class, and the conflict in my heart is very strong.
I have a good time for Java's object-oriented characteristics, and I think that I have a real understanding.
I have also begun to consciously use the OOP style to write the program, but I still don't think it is not
I know how to refine the class, when I face a specific problem, I feel that the brain is
Wan Xu, I don't know how to start, I will return to the original idea.
For example, you have to send an advertisement message, the advertisement list exists in the database.
. If you write with C, you will generally think about it, first read the email content.
, Then connect the database, loop the email address, call the native's qmail SE
The ndmail command is sent.
Then consider using Java to achieve, since it is OOP, you can't make a code.
In the main process, three classes were designed:
A class is responsible for reading the database, taking the email address, calling the send of qmail
Mail command is sent;
A class is the 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 email
class.
Put a piece of work into three modules to process, each class completes one module
task.
Carefully analyze, you will find that such a design is entirely from programmers to implement program function.
The angle is designed, or when the design is designed, it is self-bottomed.
Analyze problems from the perspective of the machine from the perspective of the real world. So when designing
The details of programming implementation have been taken into account, trying to implement procedures from the underlying
Such starting points come to meet the goals of software requirements that meet the real world.
Such an analysis method is actually not suitable for object-oriented programming languages such as Java.
Because, if you switch to C language, pack two C functions, will be achieved from Java
It's easy to easily, logically clear.
I think the object-oriented essence is to consider the problem of human thinking from the real world.
If you are accomplished, as long as you appreciate this, you will understand the object-oriented thinking.
To give a very simple example: If you need to write a webpage counter now
, The customer visits the page, the web counter plus 1, the counter is coming to access
Code:
http://hostname/count.cgi? id = xxx
There is a database table in the background, saving each ID (an ID corresponds to a statistical access
The counter current value of the number of times, request the page, the counter corresponding to the counter
Field plus 1 (here we ignore concurrent update database tables, the problem of table locks)
.
If we will analyze in accordance with the perspective of the program, we will consider this
: The first is from http
Get requests to get the ID, then follow the ID database table, get a ID corresponding to
Access the count value, then add 1, update the database, and finally display the access count.
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 to have a counter, this counter should have such a function, refresh the page
, The amount of visit will add 1, and it is best to have a function of a counter.
, Of course, if there is a function that can be set to arbitrary value
I can cheat.
As a person who has no programming experience, he will not think about the database.
How to do it, how to pass on HTTP variables, the angle of his question is me
What is the need, what is my business logic, what is the function of the software?
According to this idea (please note that his ideas is actually our usual habit
Thinking method), 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
script> setCount () // Designator is the corresponding value method
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. Counting Counter in count.cgi
The program piece is as follows:
Code:
// 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 Counter class method.
The specific code is, at this time, consider the details of the specific programming language, such as
In the getCount () method access the database, update the count value.
As seen from the above example, object-oriented thinking is actually our real life
The way of thinking of habits is to set out humanity from the perspective of human consideration.
The way of thinking is gradually translated into procedures to understand the way of thinking.
In the process of this translation, the software is gradually designed.
In the process of software design, the most probably wrong mistake is performed using the object-oriented thinking method.
That is to start analysis, I thought of the details of the program code implementation.
Therefore, the package of the package is completely based on program implementation logic, not based on problem-solving industry.
Logic.
The classic error of learning JDBC programming is: "How do I package SE to the database?
LECT operation? "
Object-oriented design is based on the design of business problems, not based on specific programming skills.
Design of surgery. I won't go to encapsulate the SELECT statement, I only package solve the problem.
Business logic, reading the database is to consider the code implementation stage of business logic.
problem.
Going back to see the example of the advertisement email, how should I apply object-oriented thinking?
How about?
For a message, there is a mail header, a mail body, and an email address.
, Send mail, need a method of sending, and one need to put all mail
The method listed. So you should design: Code:
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 Junkmail is designed, then call the Junkmail class.
The sending of mail will be very relaxed.
If the traditional process-oriented programming is the process that is in line with the machine operation instruction
So object-oriented thinking is the thinking of human solutions in real life.
script> process.
When the object-oriented software analysis and design, you must remind yourself, don't go up.
I want to implement the implementation of the program code, and I should put the constraints of the specific programming language, concentrate on analyzing me.
What do you want to implement business logic, analysis software business process, how to think
Describe and implement software business. After all, the software is just a carrier, and the business is what we really
To achieve the goal.
But in the design process, my heart is often worried. If I don't think about the procedural generation
If the code is implemented, then how do I know that my design is reasonable?
? How do I know the class I design, can the interface be implemented? So you can often see
The phenomenon is:
In the design process, although you know that you can't think about the code is prematurely, a class is designed.
, An interface, I have to evaluate my familiar programming language.
Let's see if you can make it, so you will return it to the program function.
The old road of the achievement is designed.
For example, when you do a web programming, you must often encounter page display.
The situation of data. For example, it is necessary to list all the users in the system.
. Suppose you use the User class to represent users, increase user adduser ()
Delete user deleteuser (), query all users Listus
ERS () method. And there is a USER table in the database, one record is used
Information about households. Let's take into account the implementation of the User class:
Adduser () and deleteuser () methods are good
It is to add records and delete records for the database. For Listusers (
Method, in fact, it is a SELECT for the USER table, takes out a recordset.
. But how to get a list of all users from a listUsers () method
?
The return value called by a method is only one, no multiple, so many cases
The way 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 can be implemented.
, Remove one record from the database, insert it into the vector
. In the main program, call the listUsers () method to return a V
Ector, then operate the VECTOR traversal, you can get a list of users.
Code:
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 during design
Considering the use of Java's collection class vector to implement unordered length data
The set is stored, so it violates a principle for object-oriented design: should not be in design
Premature consideration of the implementation of specific programming languages. So the abstract method must be used
And the specific implementation-independent approach to express business logic.
We know, usually use data structures with aggregation features.
EXT and HASNEXT methods, Next implementation takes off a user
Hasnext judges whether or not there is an element.
So we define an interface Iterator, defined two methods in this interface.
Next and Hasnext:
Code:
Public interface itrator {
Public Boolean Hasnext () {}
Public Object next () {}
}
The User class ListUses method returns to Iterator
Interface implementation class:
Code:
PUBLIC class user {
...
Public itrator Listusers () {
}
...
}
This is divided by the design and specific implementation method of the User class.
, Because any new () and HasNext is achieved at this time.
The class of the method can be used as a return value of Listusers
Can be used to express the "user list", not only you can use Vector
Yes. For example, I can express the list of users with arraylist.
script>, because ArrayList also implements iterator
Of course, I can also write a class to store a list of users, just implement NEX.
T () and HASNext () methods will be.
In this way, the programmer has the greatest flexibility when the specific write code is written.
However, different programming methods can be stored in different programming methods according to specific situations.
. In particular, the coupling of the program is reduced, and the portability of the program is improved.
. For Listallmail on the JUnkmail above (
The method should also be changed to the interface type.
Then, in the main program, use the User class ListUsers.
method:
Code:
User myuser = new user ();
Iterator itrator = myuser.listusers ();
While (item.hasnext ()) {
Iterator.next ();
}
This will not consider the program code completely, from the high level, abstract
Come, defined becoming an interface, and it can be designed and reasonable.
, Completely based on the needs of the business.
Conclusion
By design instructions on several examples, use object-oriented thinking methods
In fact, a process of abstracting business logic from specific programming techniques
And this abstract process is from top-down, very in line with human thinking habits.
It is also the details of the problem solving first, and the most important aspect of the problem is abstracted.
A simple framework, focus on how to solve the main contradiction, then in the process of solving the problem, then divide the details into a small problem, then dedicate
Question.
Therefore, once it is firmly grasped, you will find that during software design and development
You always use the object-oriented thinking method to design and write.
Order, and the design and development of the program will not be so boring, and a reasonable application surface
Software for designing and architecture to object technology, is also the artistic beauty of thinking.
Finally, I am willing to object-oriented thinking methods to bring your program to create music.
interest.