Java Object-Oriented Analysis
1. An important experience of object-oriented design:
Anyone who owns the data provides external methods to operate the data.
2. Typical object-oriented Cases
1. Draw circles on the blackboard
Objects involved: person, blackboard, and circle
Which object should we use to draw circles?
Because the circle method involves the center and radius of Private member variables, the circle method should be provided by the circle.
2. train drivers brake urgently
Objects involved: drivers and trains
Should the brakes be on the driver or on the train?
The brake action involves engine flameout, clutch, and wheel lock. Therefore, this method should be provided by the train.
3. The salesperson collects statistics on the amount of small tickets sold
Objects involved: salesclerk and ticket
Should the statistical method be provided by the salesperson or the bill? (Ticket)
3. Object-oriented Design Questions
1. Move the ball from one end of a rope to the other
public class Point {private float x;private float y;public Point(float x, float y) {this.x = x;this.y = y;}}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) {return null;}}class Ball {private Rope rope;private Point currentPoint;public Ball(Rope rope, Point startPoint) {this.rope = rope;this.currentPoint = startPoint;}public void move() {}}
2. Two stones are ground into a stone knife, which can cut trees, cut into wood, and make wood into chairs.
Object: Stone StoneKnife Tree Material Chair
StoneKnife s = KnifeFactory. createKnife (stone)
Material m = StoneKnife. cut (tree)
Chair c = ChairFactory. makeChair (material)