前言
在我们平时开发业务过程中经常会遇到很多if-else的判断情况,需要根据不同的情况或者不同的参数走不同的逻辑,所以大量的if-else非常的麻烦,代码十分不美观,所以可以通过相关方法解决这个问题。
使用步骤
以生成文书为例,现在有一个根据不同的参数生成不同文书的需求
一、定义生成文书接口
package com.example.demo.simplifyIfElse;
/**
* @description: 生成文书接口
* @author: hbc
* @date: 2022-07-14 15:36
*/
public interface Document {
/**
* 文书类型
* @return
*/
Integer getType();
/**
* 生成文书
*/
void createDocument();
}
二、编写具体业务实现类
1、生成目录实现类
package com.example.demo.simplifyIfElse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @description: 生成目录具体业务类
* @author: hbc
* @date: 2022-07-14 15:36
*/
@Slf4j
@Component
public class CatalogImpl implements Document {
@Override
public Integer getType() {
return 2;
}
/**
* 具体业务
*/
@Override
public void createDocument() {
log.info("执行生成目录具体业务类");
}
}
2、生成封面实现类
package com.example.demo.simplifyIfElse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @description: 生成封面具体业务类
* @author: hbc
* @date: 2022-07-14 15:36
*/
@Slf4j
@Component
public class CoverImpl implements Document {
@Override
public Integer getType() {
return 1;
}
/**
* 具体业务
*/
@Override
public void createDocument() {
log.info("执行生成封面具体业务类");
}
}
三、编写策略类
package com.example.demo.simplifyIfElse;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* @description: 生成文书策略类
* @author: hbc
* @date: 2022-07-14 15:39
*/
@Slf4j
@Component
public class DocumentRouteInitialize implements InitializingBean {
private final static EmptyDesc EMPTY = new EmptyDesc();
private static final Map<Integer, Document> routerMap = Maps.newHashMap();
@Autowired
private List<Document> documentList;
@Override
public void afterPropertiesSet() throws Exception {
for (Document service : documentList) {
routerMap.put(service.getType(), service);
}
}
public Document getService(Integer type) {
Document document = routerMap.get(type);
return Optional.ofNullable(document).orElse(EMPTY);
}
/**
* 未知的响应
*/
private static class EmptyDesc implements Document {
@Override
public Integer getType() {
return null;
}
@Override
public void createDocument() {
log.error("类型错误,生成文书失败");
}
}
}
或者使用Sping容器提供的接口简化if-else。
package com.example.demo.simplifyIfElse;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @description: 生成文书管理类
* @author: hbc
* @date: 2022-07-14 15:39
*/
@Slf4j
@Component
public class DocumentRouteApplicationContextAware implements ApplicationContextAware {
private ApplicationContext applicationContext;
private static final Map<Integer, Document> routerMap = Maps.newHashMap();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
// 准备 Document 的所有实现类
prepareDocument();
}
private void prepareDocument() {
// 获取容器中所有生成文书的实现类
Map<String, Document> documentMap = this.applicationContext.getBeansOfType(Document.class);
documentMap.forEach((key, value) -> {
routerMap.put(value.getType(), value);
});
}
public static Document getDocument(Integer type) {
return routerMap.get(type);
}
}
四、测试
@Autowired
private DocumentRouteInitialize documentRouteInitialize;
@Test
public void simpleIfElse(){
Document service = documentRouteInitialize.getService(1);
service.createDocument();
Document document = DocumentRouteApplicationContextAware.getDocument(2);
document.createDocument();
}
效果:
参数可以自定义在配置文件中或者常量等形式更加灵活。