Java object-Oriented thinking method

Source: Internet
Author: User
Tags mail qmail

I began to learn Java programming to contact OOP (object-oriented programming), just started to write programs in Java feel very awkward, because I used to use C to write programs, appreciate the simplicity and efficiency of C, like C concise and expressive ability of rich style, Especially can not endure Java run slow speed, relatively lengthy code, and a very simple thing, to write a lot of classes, a class call a class, the heart of the resistance is very strong.

I've been thinking a long while about the object-oriented nature of Java, since the thought of understanding, but also began to consciously use OOP style to write programs, but still often feel that do not know how to refine the class, in the face of a specific problem, will feel the brain of a multitude of things, do not know how to do, accidentally, And will go back to the original ideas up.

For example, to send an ad message, an ad mailing list exists inside the database. If you use C to write, you will generally think so, first read the contents of the message, and then connect the database, loop to take the mail address, call the qmail sendmail command sent.

Then consider using Java to implement, since it is OOP, can not be any code into the main process, so the design of three classes:

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

A class is read mail content, MIME encoded into HTML format, plus the message header;

A main class is responsible for reading parameters from the command, processing command-line arguments, and invoking the email class.

The work is divided into 3 modules according to the function, each class completes a module task.

Careful analysis, you will find that this design is entirely from the programmer to achieve the point of view of the program function, or, design class, is from the low up, from the machine point of view to the real world perspective to analyze the problem. Therefore, in the design, the program has been the implementation of the details are taken into account, in an attempt to achieve from the bottom of the program such a starting point to meet the real world software requirements of the goal.

This method of analysis is actually not applicable to Java object-oriented programming language, because, if the use of C language, encapsulation two C functions, will be more relaxed than Java implementation, logically also more clearly.

I think the essence of object-oriented is to consider the idea of the problem is from the real world of human thinking habits, as long as the understanding of this, we understand the object-oriented thinking method.

A very simple example: if you need to write a web counter now, the client accesses the page, the counter adds 1, and the counter is accessed http://hostname/count.cgi?id=xxx

There is a database table in the background, save each ID (an ID corresponding to a count of the number of pages) of the counter current value, request the page, the corresponding ID counter field plus 1 (here we ignore the Concurrent Update database table, the problem of table locking).

If you follow the general program implementation of the analysis, we will consider this: first, from the HTTP GET request to the ID, and then check the database table by ID, get an ID corresponding to the access count, and then add 1, update the database, and finally to the page to display the access count.

Now suppose a person who has no experience in programming, how will he think about the problem? What kind of demand will he ask? He would probably think so:

I need to have a counter, this counter should have such a function, refresh the page, the number of visits will be added 1, and the best also has a counter clear 0 function, of course, if the counter has a function can be set to any value, I can cheat.

As a person without programming experience, he could not think of how to operate the database, how to pass the HTTP variable, he considered the point of view of the problem is what I need, what my business logic is, what the software should have function.

According to this idea (please note that his idea is in fact, we usually in the habit of thinking in life), we know need to have a counter class Counter, there is a must and two optional methods:

getCount()
// 取计数器值方法
resetCounter()
// 计数器清0方法
setCount()
// 设计数器为相应的值方法
把Counter类完整的定义如下:
public class Counter
{
public int getCount(int id)
{
}
public void resetCounter(int id)
{
}
public void setCount
(int id, int currentCount)
{
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.