我有一個根本沒有重構的代碼。我在某種程度上對它進行了重構。但是我停留在一個我無法想象的地步。重構的java代碼
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();
}
還有什麼可重構...任何建議嗎?
除非您實際擁有'farm.com',否則您應該使用不同的包名稱。 – SLaks
@SLaks所有在同一包...這就是爲什麼我使用它。 – user1147717
這應該在codereview.stackexchange.com上。 –