因为几个查询条件耗时久查到结果就提前结束,但是有重复的地方觉得不够优雅 if
判断那一块
private void prepareBookingOffice(BrChangedEvent brChangedEvent) { BrGeneralInfoDto brGeneralInfoDto = Optional.ofNullable(brChangedEvent) .map(BrChangedEvent::getBrDto) .map(BrDto::getGeneralInfo) .orElse(BrGeneralInfoDto.builder().build()); if (StringUtils.isNotEmpty(brGeneralInfoDto.getBookingOfficeCode())) { return; } // 耗时 String officeCode = getOfficeCodeByOfficeUnlLocCode(brGeneralInfoDto); // 重复 if (StringUtils.isNotEmpty(officeCode)) { brGeneralInfoDto.setBookingOfficeCode(officeCode); return; } // 耗时 officeCode = getOfficeCodeByPor(brChangedEvent); // 重复 if (StringUtils.isNotEmpty(officeCode)) { brGeneralInfoDto.setBookingOfficeCode(officeCode); return; } // 耗时 officeCode = getOfficeCodeByFnd(brChangedEvent); // 重复 if (StringUtils.isNotEmpty(officeCode)) { brGeneralInfoDto.setBookingOfficeCode(officeCode); return; } // 耗时 officeCode = getOfficeCodeByPol(brChangedEvent); // 重复 if (StringUtils.isNotEmpty(officeCode)) { brGeneralInfoDto.setBookingOfficeCode(officeCode); } }
有些回复有些不太理解(知识储备不够...),后续再研究研究
目前先尝试了这位老哥的写法:
public class OptionalChain<T> { private T value; private OptionalChain(T val) { value = val; } public static <T> OptionalChain<T> of(Supplier<T> supplier) { return new OptionalChain<>(supplier.get()); } public OptionalChain<T> or(Supplier<T> supplier) { if (StringUtils.isEmpty(value)) { value = supplier.get(); } return this; } public T get() { return value; } } private void prepareBookingOffice(BrChangedEvent brChangedEvent) { BrGeneralInfoDto brGeneralInfoDto = Optional.ofNullable(brChangedEvent) .map(BrChangedEvent::getBrDto) .map(BrDto::getGeneralInfo) .orElse(BrGeneralInfoDto.builder().build()); brGeneralInfoDto.setBookingOfficeCode(OptionalChain.of(() -> brGeneralInfoDto.getBookingOfficeCode()) .or(() -> getOfficeCodeByOfficeUnlLocCode(brGeneralInfoDto)) .or(() -> getOfficeCodeByPor(brChangedEvent)) .or(() -> getOfficeCodeByFnd(brChangedEvent)) .or(() -> getOfficeCodeByPol(brChangedEvent)) .or(() -> StringUtils.EMPTY) .get()); }
补充一下:
![]() | 1 itning 2021-10-21 13:59:57 +08:00 封装 继承 多态 |
2 aguesuka 2021-10-21 14:03:37 +08:00 把耗时函数改成 lambda 保存到 List 中 |
![]() | 3 zoharSoul 2021-10-21 14:07:07 +08:00 没什么问题 挺好看懂的 |
![]() | 4 zsl199512101234 2021-10-21 14:11:03 +08:00 ![]() 二楼的方法,把耗时函数改成 lambda 保存到 list 中去,然后循环调用直到不为空就赋值并且 break |
![]() | 5 zjsxwc 2021-10-21 14:12:18 +08:00 for 循环遍历 List |
![]() | 6 chendy 2021-10-21 14:12:33 +08:00 第一步,把“取 officeCode”抽出去,如果方法不是很多而且也不怎么调整,做到这里就可以了 private String getOfficeCode(BrChangedEvent e){ String code = findOfficeCodeByA(e); if (!StringUtil.isEmpty(code)) { return code; } // 后面的差不多 } 如果方法比较多且需要调整,那么就进一步抽 private List<Function<BrChangedEvent, String>> codeFinders; { codeFinders.add(this::findOfficeCodeByA); codeFinders.add(this::findOfficeCodeByB); codeFinders.add(this::findOfficeCodeByC); } private String getOfficeCode(BrChangedEvent e) { for(Function<BrChangedEvent, String>> finder : codeFinders) { String code = finder.apply(codeFinders); if(!StringUtil.isEmpty(code)){ return code; } return null; } } |
![]() | 7 admol 2021-10-21 14:12:42 +08:00 如果你只是觉得 if 有点多,给你提供一个思路,你看是否可行 public static void main(String[] args){ String officeCode = null; if(Objects.nonNull(officeCode = a()) || Objects.nonNull(officeCode = b())|| Objects.nonNull(officeCode = c())){ }else{ officeCode = "default value"; } System.out.println("officeCode:"+officeCode); } private static String c(){ System.out.println("C"); return "C"; } private static String b(){ System.out.println("B"); return "B"; } private static String a(){ System.out.println("A"); return null; } |
![]() | 8 zsl199512101234 2021-10-21 14:14:31 +08:00 ```java @FunctionalInterface public interface OfficeCodeService { BookingOfficeCode getOfficeCode(BrGeneralInfoDto brGeneralInfoDto); } ``` |
![]() | 9 mango88 2021-10-21 14:15:15 +08:00 耗时操作有优先级要求吗 ? 还是只要任一返回值就可以了 ? 如果是任一个操作返回就满足需求,可以试试用 CompletableFuture.anyOf() |
![]() | 10 uCharles 试试抽取之后用 Optional 来进行判断 |
![]() | 12 ipwx 2021-10-21 14:18:21 +08:00 不懂 Java,伪代码 interface IAction { String run(); } class GetOfficeCodeByOfficeUnlLocCodeAction implements IAction { ... }; class GetOfficeCodeByPorAction implements IAction { ... }; 。。。 List<IAction> actiOns= { new GetOfficeCodeByOfficeUnlLocCodeAction(...), new GetOfficeCodeByPorAction(...) }; for (action in actions) { String officeCode = getOfficeCodeByOfficeUnlLocCode(brGeneralInfoDto); // 重复 if (StringUtils.isNotEmpty(officeCode)) { brGeneralInfoDto.setBookingOfficeCode(officeCode); return; } } |
![]() | 13 wolfie 2021-10-21 14:20:26 +08:00 在 Optional 基础上,定义一个 OptionalWrapper 。 OptionalWrapper<T> or(Supplier<T> supplier) 可以链式调用。 |
![]() | 14 Guiyanakuang 2021-10-21 14:23:14 +08:00 为了保证可读性不建议抽取保存到 list 里,只需要将重复部分独立为单个函数,每个函数返回 this,链式调用逻辑更清晰 |
![]() | 15 wolfie 2021-10-21 14:31:14 +08:00 ![]() ``` class OptionalChain<T> { private T value; private OptionalChain(T val) { value = val; } public static <T> OptionalChain<T> of(Supplier<T> supplier) { return new OptionalChain<>(supplier.get()); } public OptionalChain<T> or(Supplier<T> supplier) { if (value == null) { value = supplier.get(); } return this; } public T get() { return value; } } ``` |
16 hingbong 2021-10-21 16:30:40 +08:00 ``` public OptionalChain<T> or(Predicate<T> predicate, Supplier<T> supplier) { if (predicate.test()) { return this; } value = supplier.get(); }``` 判断条件也动态起来? |
17 fkdog 2021-10-21 16:30:43 +08:00 将所有的 getOfficeCodeByXXX()包装成 Function,然后塞进一个 List 。 做 list 的迭代,执行 function.apply(),如果返回值非空,则 break 跳出迭代。 ``` java List<Function<BrChangedEvent,String>> funcs= Arrays.asList( XXX::getOfficeCodeByOfficeUnlLocCode, XXX::getOfficeCodeByPor, XXX::getOfficeCodeByFnd, XXX::getOfficeCodeByPol); for(Function<BrChangedEvent,String> func:funcs){ String officeCode = func.apply(brGeneralInfoDto); if(String.isNotEmpty(officeCode)){ brGeneralInfoDto.setBookingOfficeCode(officeCode); } } ``` |
19 yazinnnn 2021-10-21 20:05:04 +08:00 functions.stream() .parallel() .map(it -> it.apply("")) .filter(StringUtils::isNotBlank) .findFirst() .orElse(""); 这样? |
![]() | 20 itechify PRO remark,这样优化也有意义也有意思,后面再添加就不断的 append 进去 |
21 crclz 2021-10-21 22:19:07 +08:00 初始实现没啥问题,第二版过拟合了(现在最简,但是不能很好应对新需求) |
![]() | 22 bxb100 2021-10-21 22:40:45 +08:00 可以考虑使用责任链设计模式 |
23 jorneyr 2021-10-21 23:04:15 +08:00 if 已经是最优解了,其他各种花哨的办法难看的要死。 |