【发布时间】:2015-06-09 12:32:46
【问题描述】:
根据业务逻辑,一个方法的输出被用作另一个方法的输入。逻辑具有线性流动。 为了模拟这种行为,现在有一个控制器类包含所有内容。
它非常混乱,loc太多且难以修改。异常处理也非常复杂。个别方法做了一些处理,但全局异常会冒出来,这涉及到很多 try catch 语句。
是否存在解决此问题的设计模式?
示例控制器类代码
try{
Logic1Inputs logic1_inputs = new Logic1Inputs( ...<some other params>... );
Logic1 l = new Logic1(logic1_inputs);
try{
Logic1Output l1Output = l.execute();
} catch( Logic1Exception l1Exception) {
// exception handling
}
Logic2Inputs logic2_inputs = new Logic2Inputs(l1Output);
Logic2 l2 = new Logic2(logic2_inputs);
try{
Logic2Output l2Output = l2.execute();
} catch( Logic2Exception l2Exception) {
// exception handling
}
Logic3Inputs logic3_inputs = new Logic3Inputs(l1Output, l2Output);
Logic3 l3 = new Logic3(logic2_inputs);
try{
Logic3Output l3Output = l3.execute();
} catch( Logic3Exception l3Exception) {
// exception handling
}
} catch(GlobalException globalEx){
// exception handling
}
【问题讨论】:
-
@user1121883 我认为您的评论(好吧,除了纯链接之外还有一些其他作品)会是一个很好的答案!
-
就目前而言,我不认为代码太糟糕了。事实上,我可以想象规范看起来和代码非常相似,如果规范很复杂,真的没什么可做的。话虽如此,您当然可以随时将其分解为更小的方法,并在适当的地方添加 cmets。
-
责任链的一些变体也可以工作
标签: java design-patterns