Object-Oriented Design Skills

Source: Internet
Author: User

1. An important experience for Object-Oriented Design:

Anyone who owns the data provides external operations on the data.

2. Object-oriented refining experience:

Term refining method.

Case:

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 think about emergency braking: Who should design the emergency braking method? Is it a driver or a 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. Thinking about how the salesperson counts the amount of small receipts: is the ticket amount counted by the conductor's function or the 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. A person closes the door. for a specific type of door, the door is composed of a series of actions. The door is rotated, directed to the door frame, and locked, the specific content should be designed for the door class if it is oriented to objects. People just call the method of closing the door. 5. moving the ball from one end of a rope to the other has two objects: the ball and the rope. How does the ball move 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,
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 next vertex of the current vertex can be calculated through the mathematical formula of the 2.1 line, this detail is not a consideration 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. the two stones are ground into a stone knife. The stone knife can cut trees, cut into wood, and make wood into chairs. This design is a little different, because we cannot find the data we have, more is a kind of production relationship. First we can think of these objects stone, stoneknife, tree, material, chair. however, 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 and define a factory to produce stoneknife = knifefactory. createknife (stone first, stone second ). for material = stoneknife. cut (tree), for wood can be converted into a chair, or other, so what is not the characteristics of wood itself. However, wood cannot be converted into a chair, and the factory Chair = chairfactory. makechair (material) needs to use the above cases to get 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. Comprehension 1: do not be too rigid during analysis. "whoever owns this attribute has the method to operate this attribute." This experience applies to many situations, this is the first thing we need to consider in our analysis. When this principle cannot solve the problem, we need to think about other methods. We must make it clear that no matter how we analyze it, the ultimate goal is to abstract objective things into a form that can be expressed with concise, exquisite, and efficient code as much as possible. Remember!

Comprehension 2:

 

Some projects, if we have no experience, cannot be done at all,

It is impossible to think quickly.

How to implement,

It depends entirely on inspiration.

Some projects depend on the spirit even if you have experience.

Sense. But there is no doubt that the more experience, the more accumulation, the more efficient

The more you get, the faster you get. Therefore, in any case, we should pay attention to the accumulation of experience.

An effective way to accumulate experience is to read and understand a lot of items that have been made by others.

Reading others' source code and learning from others' excellent ideas.

 

 

Object-Oriented Design Skills

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.