Java回退模式
发布时间:2020-09-01 18:09:31 所属栏目:Java 来源:互联网
导读:我试图找到一个很好的方式来实现一个依赖于第三方库类的服务.我还有一个“默认”实现用作回退,以防库不可用或无法提供答案. public interface Service { public Object compute1(); public Object compute2();}public class DefaultService implements Servic
|
我试图找到一个很好的方式来实现一个依赖于第三方库类的服务.我还有一个“默认”实现用作回退,以防库不可用或无法提供答案. public interface Service {
public Object compute1();
public Object compute2();
}
public class DefaultService implements Service {
@Override
public Object compute1() {
// ...
}
@Override
public Object compute2() {
// ...
}
}
服务的实际实现将是: public class ServiceImpl implements Service {
Service defaultService = new DefaultService();
ThirdPartyService thirdPartyService = new ThirdPartyService();
@Override
public Object compute1() {
try {
Object obj = thirdPartyService.customCompute1();
return obj != null ? obj : defaultService.compute1();
}
catch (Exception e) {
return defaultService.compute1();
}
}
@Override
public Object compute2() {
try {
Object obj = thirdPartyService.customCompute2();
return obj != null ? obj : defaultService.compute2();
}
catch (Exception e) {
return defaultService.compute2();
}
}
}
目前的实现似乎重复了一些事情,只有对服务的实际调用是不同的,但是try / catch和默认机制几乎相同.此外,如果在服务中添加了另一种方法,那么实现将看起来几乎相同. 是否有可能在这里应用的设计模式(proxy,strategy),使代码看起来更好,进一步添加更少的复制粘贴? 解决方法您可以使用方法引用将常用逻辑提取为单独的方法,如:public class ServiceImpl implements Service {
Service defaultService = new DefaultService();
ThirdPartyService thirdPartyService = new ThirdPartyService();
@Override
public Object compute1() {
return run(thirdPartyService::customCompute1,defaultService::compute1);
}
@Override
public Object compute2() {
return run(thirdPartyService::customCompute2,defaultService::compute2);
}
private static <T> T run(Supplier<T> action,Supplier<T> fallback) {
try {
T result = action.get();
return result != null ? result : fallback.get();
} catch(Exception e) {
return fallback.get();
}
}
} (编辑:十堰站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
