[code]
/*面向对象设计把握一个重要的经验:谁拥有数据,谁就对外提供操作这些数据的方法
1.人在黑板上画圆
2.列车司机紧急刹车
3.售货员统计收获小票的金额
4.你把门关上了
5。球从一根绳子的一段移动到了另一端
*/
1.对于这个设计,上面有person , blackborad , circle三个对象,
动词 : 画。
因为画的动作的数据 x,y,randius 是circle拥有的数据,所以 draw()方法应该属于circle对象。。。而不是动作的发出者。。而发出者往往会委托动作承受者
2。 司机发出的刹车动作会委托车本身来完成
3。售货员统计小票的动作会委托小票来完成
4。关门的动作会委托门本身来完成。
5。详解:
/*注意点:球的移动是需要修改球本身的参数,所以方法由球本身来提供,但是他参数的获取是通过委托绳子来完成,所以任何两个事物之间的交换最好使用委托模式
*/
class Ball {
private int currentPos;
private Rope rope;
public Ball(){
rope = new Rope();
}
public void move(){
currentPos = rope.nextPoint(currentPos);
}
}
class Rope {
private int start;
private int end;
public Rope (int start , int end){
this.start = start;
this.end = end;
}
public int nextPoint(int currentPos){
if(currentPos >=end) return end;
else if(currentPos <start)return start;
else return ++currentPos;
}
}
[/code]
/*面向对象设计把握一个重要的经验:谁拥有数据,谁就对外提供操作这些数据的方法
1.人在黑板上画圆
2.列车司机紧急刹车
3.售货员统计收获小票的金额
4.你把门关上了
5。球从一根绳子的一段移动到了另一端
*/
1.对于这个设计,上面有person , blackborad , circle三个对象,
动词 : 画。
因为画的动作的数据 x,y,randius 是circle拥有的数据,所以 draw()方法应该属于circle对象。。。而不是动作的发出者。。而发出者往往会委托动作承受者
2。 司机发出的刹车动作会委托车本身来完成
3。售货员统计小票的动作会委托小票来完成
4。关门的动作会委托门本身来完成。
5。详解:
/*注意点:球的移动是需要修改球本身的参数,所以方法由球本身来提供,但是他参数的获取是通过委托绳子来完成,所以任何两个事物之间的交换最好使用委托模式
*/
class Ball {
private int currentPos;
private Rope rope;
public Ball(){
rope = new Rope();
}
public void move(){
currentPos = rope.nextPoint(currentPos);
}
}
class Rope {
private int start;
private int end;
public Rope (int start , int end){
this.start = start;
this.end = end;
}
public int nextPoint(int currentPos){
if(currentPos >=end) return end;
else if(currentPos <start)return start;
else return ++currentPos;
}
}
[/code]