来源:大飞学习笔记
链接:u6.gg/k376d
版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢!
往期惊喜:
Spring 注解 @bean 和 @component 的区别, 你知道吗?
Maven官宣:干掉Maven和Gradle!推出更强更快更牛逼的新一代构建工具,炸裂!
CTO 说了,用错@Autowired 和@Resource 的人可以领盒饭了
扫码关注我们的Java架构师技术
带你全面深入Java
大家好,我是Java架构师
在实际的业务开发当中,经常会遇到复杂的业务逻辑,可能部分同学实现出来的代码并没有什么问题,但是代码的可读性很差。
本篇文章主要总结一下自己在实际开发中如何避免大面积的 if-else 代码块的问题。补充说明一点,不是说 if-else 不好,而是多层嵌套的 if-else 导致代码可读性差、维护成本高等问题。
public class BadCodeDemo {
private void getBadCodeBiz(Integer city, List<TestCodeData> newDataList, List<TestCodeData> oldDataList) {
if (city != null) {
if (newDataList != null && newDataList.size() > 0) {
TestCodeData newData = newDataList.stream().filter(p -> {
if (p.getIsHoliday() == 1) {
return true;
}
return false;
}).findFirst().orElse(null);
if (newData != null) {
newData.setCity(city);
}
}
} else {
if (oldDataList != null && newDataList != null) {
List<TestCodeData> oldCollect = oldDataList.stream().filter(p -> {
if (p.getIsHoliday() == 1) {
return true;
}
return false;
}).collect(Collectors.toList());
List<TestCodeData> newCollect = newDataList.stream().filter(p -> {
if (p.getIsHoliday() == 1) {
return true;
}
return false;
}).collect(Collectors.toList());
if (newCollect != null && newCollect.size() > 0 && oldCollect != null && oldCollect.size() > 0) {
for (TestCodeData newPO : newCollect) {
if (newPO.getStartTime() == 0 && newPO.getEndTime() == 12) {
TestCodeData po = oldCollect.stream().filter(p -> p.getStartTime() == 0
&& (p.getEndTime() == 12 || p.getEndTime() == 24)).findFirst().orElse(null);
if (po != null) {
newPO.setCity(po.getCity());
}
} else if (newPO.getStartTime() == 12 && newPO.getEndTime() == 24) {
TestCodeData po = oldCollect.stream().filter(
p -> (p.getStartTime() == 12 || p.getStartTime() == 0)
&& p.getEndTime() == 24).findFirst().orElse(null);
if (po != null) {
newPO.setCity(po.getCity());
}
} else if (newPO.getStartTime() == 0 && newPO.getEndTime() == 24) {
TestCodeData po = oldCollect.stream().filter(
p -> p.getStartTime() == 0 && p.getEndTime() == 24).findFirst().orElse(null);
if (po == null) {
po = oldCollect.stream().filter(
p -> p.getStartTime() == 0 && p.getEndTime() == 12).findFirst().orElse(null);
}
if (po == null) {
po = oldCollect.stream().filter(
p -> p.getStartTime() == 12 && p.getEndTime() == 24).findFirst().orElse(null);
}
if (po != null) {
newPO.setCity(po.getCity());
}
} else if (newPO.getTimeUnit().equals(Integer.valueOf(1))) {
TestCodeData po = oldCollect.stream().filter(
e -> e.getTimeUnit().equals(Integer.valueOf(1))).findFirst().orElse(null);
if (po != null) {
newPO.setCity(po.getCity());
}
}
}
}
}
}
}
}
技巧一:提取方法,拆分逻辑✦
if(null != city) {
} else {
}
private void getCityNotNull(Integer city, List<TestCodeData> newDataList) {
if (newDataList != null && newDataList.size() > 0) {
TestCodeData newData = newDataList.stream().filter(p -> {
if (p.getIsHoliday() == 1) {
return true;
}
return false;
}).findFirst().orElse(null);
if (newData != null) {
newData.setCity(city);
}
}
}
// 合并逻辑流程
private void getBadCodeBiz(Integer city, List<TestCodeData> newDataList, List<TestCodeData> oldDataList) {
if (city != null) {
this.getCityNull(city, newDataList);
} else {
//此处代码省略
}
}
技巧二:分支逻辑提前return✦
public void getCityNotNull(Integer city, List<TestCodeData> newDataList) {
if (CollectionUtils.isEmpty(newDataList)) {
// 提前判断,返回业务逻辑
return;
}
TestCodeData newData = newDataList.stream().filter(p -> {
if (p.getIsHoliday() == 1) {
return true;
}
return false;
}).findFirst().orElse(null);
if (null != newData) {
newData.setCity(city);
}
}
技巧三:枚举✦
public class BadCodeDemo {
public void getBadCodeBiz(Integer city, List<TestCodeData> newDataList, List<TestCodeData> oldDataList) {
if (city != null) {
this.getCityNotNull(city, newDataList);
} else {
this.getCityNull(newDataList, oldDataList);
}
}
private void getCityNotNull(Integer city, List<TestCodeData> newDataList) {
if (CollectionUtils.isEmpty(newDataList)) {
// 提前判断,返回业务逻辑
return;
}
TestCodeData newData = newDataList.stream().filter(p -> {
if (p.getIsHoliday() == 1) {
return true;
}
return false;
}).findFirst().orElse(null);
if (null != newData) {
newData.setCity(city);
}
}
private void getCityNull(List<TestCodeData> newDataList, List<TestCodeData> oldDataList) {
// 提前判断,返回业务逻辑
if (CollectionUtils.isEmpty(oldDataList) && CollectionUtils.isEmpty(newDataList)) {
return;
}
List<TestCodeData> oldCollect = oldDataList.stream().filter(p -> {
if (p.getIsHoliday() == 1) {
return true;
}
return false;
}).collect(Collectors.toList());
List<TestCodeData> newCollect = newDataList.stream().filter(p -> {
if (p.getIsHoliday() == 1) {
return true;
}
return false;
}).collect(Collectors.toList());
// 提前判断,返回业务逻辑
if (CollectionUtils.isEmpty(newCollect) && CollectionUtils.isEmpty(oldCollect)) {
return;
}
for (TestCodeData newPO : newCollect) {
if (newPO.getStartTime() == 0 && newPO.getEndTime() == 12) {
TestCodeData po = oldCollect.stream().filter(p -> p.getStartTime() == 0
&& (p.getEndTime() == 12 || p.getEndTime() == 24)).findFirst().orElse(null);
if (po != null) {
newPO.setCity(po.getCity());
}
} else if (newPO.getStartTime() == 12 && newPO.getEndTime() == 24) {
TestCodeData po = oldCollect.stream().filter(
p -> (p.getStartTime() == 12 || p.getStartTime() == 0)
&& p.getEndTime() == 24).findFirst().orElse(null);
if (po != null) {
newPO.setCity(po.getCity());
}
} else if (newPO.getStartTime() == 0 && newPO.getEndTime() == 24) {
TestCodeData po = oldCollect.stream().filter(
p -> p.getStartTime() == 0 && p.getEndTime() == 24).findFirst().orElse(null);
if (po == null) {
po = oldCollect.stream().filter(
p -> p.getStartTime() == 0 && p.getEndTime() == 12).findFirst().orElse(null);
}
if (po == null) {
po = oldCollect.stream().filter(
p -> p.getStartTime() == 12 && p.getEndTime() == 24).findFirst().orElse(null);
}
if (po != null) {
newPO.setCity(po.getCity());
}
} else if (newPO.getTimeUnit().equals(Integer.valueOf(1))) {
TestCodeData po = oldCollect.stream().filter(
e -> e.getTimeUnit().equals(Integer.valueOf(1))).findFirst().orElse(null);
if (po != null) {
newPO.setCity(po.getCity());
}
}
}
}
}
if (newPO.getStartTime() == 0 && newPO.getEndTime() == 12) {
//第一段逻辑
} else if (newPO.getStartTime() == 12 && newPO.getEndTime() == 24) {
//第二段逻辑
} else if (newPO.getStartTime() == 0 && newPO.getEndTime() == 24) {
//第三段逻辑
} else if (newPO.getTimeUnit().equals(Integer.valueOf(1))) {
//第四段逻辑
}
public enum TimeEnum {
AM("am", "上午") {
@Override
public void setCity(TestCodeData data, List<TestCodeData> oldDataList) {
TestCodeData po = oldDataList.stream().filter(p -> p.getStartTime() == 0
&& (p.getEndTime() == 12 || p.getEndTime() == 24)).findFirst().orElse(null);
if (null != po) {
data.setCity(po.getCity());
}
}
},
PM("pm", "下午") {
@Override
public void setCity(TestCodeData data, List<TestCodeData> oldCollect) {
TestCodeData po = oldCollect.stream().filter(
p -> (p.getStartTime() == 12 || p.getStartTime() == 0)
&& p.getEndTime() == 24).findFirst().orElse(null);
if (po != null) {
data.setCity(po.getCity());
}
}
},
DAY("day", "全天") {
@Override
public void setCity(TestCodeData data, List<TestCodeData> oldCollect) {
TestCodeData po = oldCollect.stream().filter(
p -> p.getStartTime() == 0 && p.getEndTime() == 24).findFirst().orElse(null);
if (po == null) {
po = oldCollect.stream().filter(
p -> p.getStartTime() == 0 && p.getEndTime() == 12).findFirst().orElse(null);
}
if (po == null) {
po = oldCollect.stream().filter(
p -> p.getStartTime() == 12 && p.getEndTime() == 24).findFirst().orElse(null);
}
if (po != null) {
data.setCity(po.getCity());
}
}
},
HOUR("hour", "小时") {
@Override
public void setCity(TestCodeData data, List<TestCodeData> oldCollect) {
TestCodeData po = oldCollect.stream().filter(
e -> e.getTimeUnit().equals(Integer.valueOf(1))).findFirst().orElse(null);
if (po != null) {
data.setCity(po.getCity());
}
}
};
public abstract void setCity(TestCodeData data, List<TestCodeData> oldCollect);
private String code;
private String desc;
TimeEnum(String code, String desc) {
this.code = code;
this.desc = desc;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
for (TestCodeData data : newCollect) {
if (data.getStartTime() == 0 && data.getEndTime() == 12) {
TimeEnum.AM.setCity(data, oldCollect);
} else if (data.getStartTime() == 12 && data.getEndTime() == 24) {
TimeEnum.PM.setCity(data, oldCollect);
} else if (data.getStartTime() == 0 && data.getEndTime() == 24) {
TimeEnum.DAY.setCity(data, oldCollect);
} else if (data.getTimeUnit().equals(Integer.valueOf(1))) {
TimeEnum.HOUR.setCity(data, oldCollect);
}
}
for (TestCodeData data : newCollect) {
String code = "am"; // 这里假设 code 变量是从 data 中获取的
TimeEnum.valueOf(code).setCity(data, oldCollect);
}
技巧四:函数式接口✦
业务场景描述:比如让你做一个简单的营销拉新活动,这个活动投放到不同的渠道,不同渠道过来的用户奖励不一样。
@RestController
@RequestMapping("/activity")
public class ActivityController {
@Resource
private AwardService awardService;
@PostMapping("/reward")
public void reward(String userId, String source) {
if ("toutiao".equals(source)) {
awardService.toutiaoReward(userId);
} else if ("wx".equals(source)) {
awardService.wxReward(userId);
}
}
}
@Service
public class AwardService {
private static final Logger log = LoggerFactory.getLogger(AwardService.class);
public Boolean toutiaoReward(String userId) {
log.info("头条渠道用户{}奖励50元红包!", userId);
return Boolean.TRUE;
}
public Boolean wxReward(String userId) {
log.info("微信渠道用户{}奖励100元红包!", userId);
return Boolean.TRUE;
}
}
看完这段代码,逻辑上是没有什么问题的。但它有一个隐藏的缺陷,如果后期又增加很多渠道的时候,你该怎么办?继续 else if 吗?
@RestController
@RequestMapping("/activity")
public class ActivityController {
@Resource
private AwardService awardService;
@PostMapping("/reward")
public void reward(String userId, String source) {
awardService.getRewardResult(userId, source);
}
}
@Service
public class AwardService {
private static final Logger log = LoggerFactory.getLogger(AwardService.class);
private Map<String, BiFunction<String, String, Boolean>> sourceMap = new HashMap<>();
@PostConstruct
private void dispatcher() {
sourceMap.put("wx", (userId, source) -> this.wxReward(userId));
sourceMap.put("toutiao", (userId, source) -> this.toutiaoReward(userId));
}
public Boolean getRewardResult(String userId, String source) {
BiFunction<String, String, Boolean> result = sourceMap.get(source);
if (null != result) {
return result.apply(userId, source);
}
return Boolean.FALSE;
}
private Boolean toutiaoReward(String userId) {
log.info("头条渠道用户{}奖励50元红包!", userId);
return Boolean.TRUE;
}
private Boolean wxReward(String userId) {
log.info("微信渠道用户{}奖励100元红包!", userId);
return Boolean.TRUE;
}
}
针对一些复杂的业务场景,业务参数很多时,可以利用 @FunctionalInterface 自定义函数式接口来满足你的业务需求,使用原理和本例并无差别。
技巧五:设计模式✦
设计模式对于 if-else 的优化,我个人觉得有些重,但是也是一种优化方式。设计模式适合使用在大的业务流程和场景中使用,针对代码块中的 if-else 逻辑优化不推荐使用。
常用的设计模式有:
策略模式
模板方法
工厂模式
单例模式
还是以上面的营销拉新活动为例来说明如何使用。
public abstract class AwardAbstract {
public abstract Boolean award(String userId);
}
// 头条渠道发放奖励业务
public class TouTiaoAwardService extends AwardAbstract {
@Override
public Boolean award(String userId) {
log.info("头条渠道用户{}奖励50元红包!", userId);
return Boolean.TRUE;
}
}
// 微信渠道发放奖励业务
public class WeChatAwardService extends AwardAbstract {
@Override
public Boolean award(String userId) {
log.info("微信渠道用户{}奖励100元红包!", userId);
return Boolean.TRUE;
}
}
public class AwardFactory {
public static AwardAbstract getAwardInstance(String source) {
if ("toutiao".equals(source)) {
return new TouTiaoAwardService();
} else if ("wx".equals(source)) {
return new WeChatAwardService();
}
return null;
}
}
@PostMapping("/reward2")
public void reward2(String userId, String source) {
AwardAbstract instance = AwardFactory.getAwardInstance(source);
if (null != instance) {
instance.award(userId);
}
}
还是以营销拉新为业务场景来说明,这个业务流程再增加一些复杂度,比如发放奖励之前要进行身份验证、风控验证等一些列的校验,此时你的业务流程该如何实现更清晰简洁呢?另外,搜索公众号技术社区后台回复“壁纸”,获取一份惊喜礼包。
/** 策略业务接口 */
public interface AwardStrategy {
/**
* 奖励发放接口
*/
Map<String, Boolean> awardStrategy(String userId);
/**
* 获取策略标识,即不同渠道的来源标识
*/
String getSource();
}
public abstract class BaseAwardTemplate {
private static final Logger log = LoggerFactory.getLogger(BaseAwardTemplate.class);
//奖励发放模板方法
public Boolean awardTemplate(String userId) {
this.authentication(userId);
this.risk(userId);
return this.awardRecord(userId);
}
//身份验证
protected void authentication(String userId) {
log.info("{} 执行身份验证!", userId);
}
//风控
protected void risk(String userId) {
log.info("{} 执行风控校验!", userId);
}
//执行奖励发放
protected abstract Boolean awardRecord(String userId);
}
@Slf4j
@Service
public class ToutiaoAwardStrategyService extends BaseAwardTemplate implements AwardStrategy {
/**
* 奖励发放接口
*/
@Override
public Boolean awardStrategy(String userId) {
return super.awardTemplate(userId);
}
@Override
public String getSource() {
return "toutiao";
}
/**
* 具体的业务奖励发放实现
*/
@Override
protected Boolean awardRecord(String userId) {
log.info("头条渠道用户{}奖励50元红包!", userId);
return Boolean.TRUE;
}
}
@Slf4j
@Service
public class WeChatAwardStrategyService extends BaseAwardTemplate implements AwardStrategy {
/**
* 奖励发放接口
*/
@Override
public Boolean awardStrategy(String userId) {
return super.awardTemplate(userId);
}
@Override
public String getSource() {
return "wx";
}
/***
* 具体的业务奖励发放实现
*/
@Override
protected Boolean awardRecord(String userId) {
log.info("微信渠道用户{}奖励100元红包!", userId);
return Boolean.TRUE;
}
}
@Component
public class AwardStrategyFactory implements ApplicationContextAware {
private final static Map<String, AwardStrategy> MAP = new HashMap<>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, AwardStrategy> beanTypeMap = applicationContext.getBeansOfType(AwardStrategy.class);
beanTypeMap.values().forEach(strategyObj -> MAP.put(strategyObj.getSource(), strategyObj));
}
/**
* 对外统一暴露的工厂方法
*/
public Boolean getAwardResult(String userId, String source) {
AwardStrategy strategy = MAP.get(source);
if (Objects.isNull(strategy)) {
throw new RuntimeException("渠道异常!");
}
return strategy.awardStrategy(userId);
}
/**
* 静态内部类创建单例工厂对象
*/
private static class CreateFactorySingleton {
private static AwardStrategyFactory factory = new AwardStrategyFactory();
}
public static AwardStrategyFactory getInstance() {
return CreateFactorySingleton.factory;
}
}
@RestController
@RequestMapping("/activity")
public class ActivityController {
@PostMapping("/reward3")
public void reward3(String userId, String source) {
AwardStrategyFactory.getInstance().getAwardResult(userId, source);
}
}
2022-02-20 12:23:27.716 INFO 20769 --- [nio-8080-exec-1] c.a.c.e.o.c.p.s.BaseAwardTemplate : fei 执行身份验证!
2022-02-20 12:23:27.719 INFO 20769 --- [nio-8080-exec-1] c.a.c.e.o.c.p.s.BaseAwardTemplate : fei 执行风控校验!
2022-02-20 12:23:27.719 INFO 20769 --- [nio-8080-exec-1] a.c.e.o.c.p.s.WeChatAwardStrategyService : 微信渠道用户fei奖励100元红包!
其他技巧✦
使用三目运算符
相同业务逻辑提取复用
写在最后✦
不论使用那种技巧,首先是我们在业务代码开发过程中一定要多思考,将复杂的业务逻辑能通过简洁的代码表现出来,这才是你的核心能力之一,而不是一个 curd boy。与君共勉,共同进步!
最后,整理了100多套项目,赠送读者。扫码下方二维码,后台回复【赚钱】即可获取。
--END--
来源:大飞学习笔记
链接:u6.gg/k376d
版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢!
往期惊喜:
Spring 注解 @bean 和 @component 的区别, 你知道吗?
Maven官宣:干掉Maven和Gradle!推出更强更快更牛逼的新一代构建工具,炸裂!
CTO 说了,用错@Autowired 和@Resource 的人可以领盒饭了
扫码关注我们的Java架构师技术
带你全面深入Java