一个问题是为什么我想重构我的代码?
你希望它是
- 更干净的代码?
- 更模块化?
- 步骤必须在运行时可配置(可替换)吗?
重构,以使代码干净
如果步骤不需要在运行时进行配置,你想使你的代码更干净的比你应该看一看的评论你制作。每条评论都是一个提示。
步骤
/**
* 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