假设我在所有控制器中都有这些代码行:
public View ControllerClass() {
// ...
// some code in controller
SomeClass someClass;
try {
someClass = Util.getParam(
context.getEncryptedParam(), soemthignElse.getSomething());
} catch (SomeException ex) {
log.error(ex);
return viewBuilderFactory.view1.view();
} catch (AnotherException ex) {
return viewBuilderFactory.view2.view();
} catch (etc ...) {}
// use someClass
// ...
return viewBuilderFactory.view3.view();
}
在这种情况下,如果我想将复制移动到自己的方法,我会有两种不同的返回类型(void
和view
)。这里有什么好方法?
答案 0 :(得分:0)
您的代码最好按以下方式进行重组:
public View ControllerClass() {
ViewBuilderFactoryView viewBuilderFactoryView;
try {
SomeClass someClass = Util.getParam(
context.getEncryptedParam(), soemthignElse.getSomething());
// use someClass
// ...
viewBuilderFactoryView = viewBuilderFactory.view3;
} catch (SomeException ex) {
log.error(ex);
viewBuilderFactoryView = viewBuilderFactory.view1;
} catch (AnotherException ex) {
viewBuilderFactoryView = viewBuilderFactory.view2;
} catch (etc ...) {}
return viewBuilderFactoryView.view();
}
换句话说,如果您成功获得SomeClass
,请继续使用它,然后返回一些View
。如果您未成功获得SomeClass
,则只需返回一些View
。