Object-Oriented Design has an important experience: anyone who owns the data provides external methods to operate the data.
It is best to understand the case. The following describes several cases:
1. Draw circles on the blackboard
Through this sentence, it is easy to think of the objects "person", "Blackboard", and "circle". The next step is to draw the circle draw (). Which class should the method be designed?
To draw a circle, we need to know the center and the radius, and the two data are circles. To draw a circle, we need to operate the data of the center and the radius. According to my first principle, this method should be designed to be provided by circles.
2. train drivers brake urgently
Think: Who should the emergency brake approach be designed for? driver or train?
It should be designed on the train because the brake function can only be implemented by the train itself. The driver only calls the brake of the train and then enables the train to brake internally.
3. The salesperson counts the amount of the receipt ticket
Thinking: is counting the ticket amount a ticket conductor's function or a ticket sales function?
The ticket information is displayed on the ticket for sale. The salesperson calls the gettotalmoney () method of the ticket for sale object, and the gettotalmoney () method internally calculates the total amount of the ticket for sale.
4. You closed the door.
The door closing action is composed of a series of actions for the specific category of the door. The door is rotated, directed to the door frame, and locked. The specific content is subject to object-oriented thinking, it should be designed on the door class. People just call the method of closing the door.
5. Move the ball from a piece of rope to the other end
There are two objects: the ball and the rope. How can we move the ball on the rope?
Moving from one end of the rope to the other, that is, the movement of the ball on the rope changes, so we can use the seat change of the ball on the rope to represent the movement of the ball, the coordinates are defined on the rope. The method of coordinate change is only clear about the rope. The return value is the next coordinate, at the beginning, the ball should be at a certain point of a rope. The method of moving the ball is actually to call the method of changing coordinates of the rope where it is located. Code:
[Java]View plaincopy
Public class rope
{
Private point start;
Private point end;
Public rope (point start, point end)
{
This. Start = start;
This. End = end;
}
Public point nextpoint (point currentpoint)
{
/*
* The mathematical formula of the 2.1 line can be used to calculate the next vertex of the current vertex. This detail is not considered in the design phase.
* If the current vertex ends, null is returned. If the current vertex is not an online vertex, an exception is thrown.
*/
Return NULL;
}
}
Class ball
{
Private rope;
Private point currentpoint;
Public ball (rope, point currentpoint)
{
This. Rope = rope;
This. currentpoint = currentpoint;
}
Public void move ()
{
Currentpoint = rope. nextpoint (currentpoint );
System. Out. println ("Move the ball to" + currentpoint );
}
}
6. Two stones are ground into a stone knife, which can cut trees, cut into wood, and make wood into chairs.
This design is a bit different, because the data cannot be found, it is more of a production relationship.
First, we can think of these objects stone, stoneknife, tree, material, chair. But it is a little difficult for specific methods.
Stone becomes a stone knife. Does stone have a way to turn itself into stoneknife? Then he will not lose himself. This design is unreasonable, because it seems that stone knives are not the characteristics of stone itself in the object-oriented perspective.
Here we design this by defining a factory to produce stoneknife = knifefactory. createknife (stone first, stone second ).
For material = stoneknife. Cut (tree), wood can be converted into chairs or other materials. In this way, it is not the characteristics of wood itself.
However, wood cannot be converted into chairs, and the factory Chair = chairfactory. makechair (material) is required)
Through the above cases, we have a sense of object-oriented design. First, we have to look at what specific objects are, and what attributes (data) each object has, and then think about the relationship between objects from a higher level of abstract thinking.