0

I have a code which was not refactored at all. I refactored it to some extent..but stuck at a point where I cannot think of anything further.

Tractor.java:

   package com.farm;

public class Tractor implements MethodsInterface{

private int[] position;

private int[] field;

private String orientation;

public Tractor(){
    position  = new int[]{0,0};
    field = new int[]{5,5};
    orientation = "N";
}

public void move(String command) {
if(command=="F"){
moveForwards();
}else if(command=="T"){
turnClockwise();
}

}

private void moveForwards() {
if(orientation=="N"){
position = new int[]{position[0], position[1]+1}; }else if(orientation == "E"){ position = new int[]{position[0]+1, position[1]}; }else if(orientation == "S"){ position = new int[]{position[0], position[1]-1}; }else if(orientation == "W"){ position = new int[]{position[0]-1, position[1]}; } if(position[0]>field[0]||position[1]>field[1]){

try {
    throw new TractorInDitchException();
} catch (TractorInDitchException e) {
    e.printStackTrace();
}

}

}

private void turnClockwise() {
if(orientation=="N"){
orientation = "E";
}else if(orientation == "E"){
orientation = "S";
}else if(orientation == "S"){
orientation = "W";
}else if(orientation == "W"){
orientation = "N";
}
}


public int getPositionX() {
return position[0];
}

public int getPositionY() {
return position[1];
}

public String getOrientation() {
return orientation;
}
}

TractorInDitchException.java

package com.farm;

public class TractorInDitchException extends Exception{

}

MethodsInterface.java

package com.farm;

public interface MethodsInterface {

    public int getPositionX();
    public int getPositionY();
    public String getOrientation();
}

What else could be refactored...any suggestions please?

4

1 回答 1

1

我会覆盖Exception你的所有构造函数TractorInDitchException

它没有在任何地方使用。什么会导致您抛出该异常?

您可以顺时针或逆时针转动,控制比罗盘点更精细。我会重写该方法以传递航向角的增量。

为什么是硬连线的位置和场阵列?将它们传递给构造函数。说明它们的含义。

这里没有太多抽象。我可以想到很多关于拖拉机的其他事情:速度矢量、加速度、重量、燃料消耗率、牵引能力等。这对我来说就像一个贫血的域模型。工作中几乎没有想象力。

于 2012-01-24T01:46:52.047 回答