Object-oriented thinking

zhaozj2021-02-16  48

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