谁拥有数据,谁就对外提供操作这些数据的方法。
例子:
1、人在黑板上画圆 (person blackboard circle)draw(x,y) 写在circle中
2、列车司机紧急刹车
3、售货员统计收获小票的金额
4、你把门关上了
5、球从绳子上一边移动到另一边。
class Ball
{
private Rope rope;
private Point currentPoint;
public Ball(Rope rope,Point startPoint)
{
this.rope = rope;
this.currentPoint = startPoint;
}
public void move()
{
this.currentPoint = rope.nextPoint(currentPoint);
System.out.println("球移动到" + currentPoint);
}
}
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)
{
//下一个point的计算
}
}
6、两块石头磨成一把石刀,石刀可以砍树,砍成木材,木材做成椅子
Stone
StoneKnife
两块石头 变成 一把石刀(石头对象消失了)
StoneKnife = KnifeFactory.createKnife(Stone first,Stone second)
Matertial m = cut(Tree tree)
Tree
Matertial
Chair
木材变成椅子(木材消失了)
Chair c = ChairFactory.makeChair(Matertial m);