一個問題是爲什麼我想重構我的代碼?
你希望它是
- 更乾淨的代碼?
- 更模塊化?
- 步驟必須在運行時可配置(可替換)嗎?
重構,以使代碼乾淨
如果步驟不需要在運行時進行配置,你想使你的代碼更乾淨的比你應該看一看的評論你製作。每條評論都是一個提示。
步驟
/**
* Explain what step1 does.
*/
private void step1() {
// Step 1
Map<String, SomeObject> objectsMap = someService.createObjectsMap();
if (!objectsMap.isEmpty()) {
step2(objectsMap);
} else { // Step 1 failed
// log and handle
}
}
/**
* Explain what step2 does.
*/
private void step2(Map<String, SomeObject> objectsMap) {
// Step 2
final AnotherObject anotherObject = anotherService
.createAnotherObject(objectsMap);
if (null != anotherObject) {
step3(anotherObject);
} else { // Step 2 failed
// log and handle
}
}
/**
* Explain what step3 does.
*/
private void step3(AnotherObject anotherObject) {
// Step 3 that gets anotherObject as input and returns something
// else
}
這種方法只是分解成更小的方法的方法後,打破代碼塊到方法,並將它們命名。優點是每個較小的方法只對一件事負責。而且因爲這是一種方法,所以可以將javadoc添加到它。所以不再需要內嵌評論。
爲了使步驟在運行時
更換如果你想配置比你必須封裝它們中的對象在運行時(例如,由於一些用戶輸入的)執行的步驟,因爲重構你的應用程序引用了可以替換的對象。
既然你想要所有的步驟都有一個共同的API,你必須使它更通用。
從客戶的角度開始思考。這些步驟應該如何執行。例如。
for (Step step : steps) {
boolean executeNext = step.execute();
if (!executeNext) {
break;
}
}
設計一個步驟接口
public interface Step {
boolean execute();
}
如何通過一個步驟作爲輸入到另一個的輸出?
做一個接口
public static interface StepInput<T> {
public T getInput();
}
實現你的腳步。抽象類將幫助你。
public abstract class InputOutputStep<T> implements Step,
StepInput<T> {
private T returnValue;
protected void setReturnValue(T returnValue) {
this.returnValue = returnValue;
}
public T getInput() {
return returnValue;
}
}
public class Step1 extends InputOutputStep<Map<String, SomeObject>> {
private StepInput<Map<String, SomeObject>> stepInput;
public Step1(StepInput<Map<String, SomeObject>> stepInput) {
this.stepInput = stepInput;
}
public boolean execute() {
boolean executeNext = false;
Map<String, SomeObject> objectsMap = stepInput.getInput();
if (!objectsMap.isEmpty()) {
// Step 2
setReturnValue(objectsMap);
executeNext = true;
} else { // Step 1 failed
// log and handle
}
return executeNext;
}
}
public class Step2 extends InputOutputStep<AnotherObject> {
private StepInput<Map<String, SomeObject>> stepInput;
private AnotherService anotherService;
public Step2(AnotherService anotherService,
StepInput<Map<String, SomeObject>> stepInput) {
this.anotherService = anotherService;
this.stepInput = stepInput;
}
public boolean execute() {
boolean executeNext = false;
Map<String, SomeObject> objectsMap = stepInput.getInput();
AnotherObject anotherObject = anotherService
.createAnotherObject(objectsMap);
if (null != anotherObject) {
setReturnValue(anotherObject);
executeNext = true;
} else { // Step 2 failed
// log and handle
}
return executeNext;
}
}
public class Step3 extends InputOutputStep<Void> {
private StepInput<AnotherObject> stepInput;
public Step3(StepInput<AnotherObject> stepInput) {
this.stepInput = stepInput;
}
public boolean execute() {
AnotherObject anotherObject = stepInput.getInput();
setReturnValue(null);
return false;
}
}
在運行時配置的步驟和執行
Step1 step1 = new Step1(stepInput);
Step2 step2 = new Step2(anotherService, step1);
Step step3 = new Step3(step2);
Step[] steps = new Step[]{step1, step2, step3};
for (Step step : steps) {
boolean executeNext = step.execute();
if (!executeNext) {
break;
}
}
'try' /'catch'? – immibis 2014-10-08 06:17:20
怎麼樣? – forhas 2014-10-08 06:17:55
它似乎正是你想要的。 (好吧,你也需要拋出) – immibis 2014-10-08 06:27:05