@Service
public class QueryGrantTypeService {
@Autowired
private GrantTypeSerive grantTypeSerive;
private final Map<String, Function<String, String>> grantTypeMap = new HashMap<>();
/**
* 初始化业务分派逻辑,代替了if-else部分
* key: 优惠券类型
* value: lambda表达式,最终会获得该优惠券的发放方式
*/
@PostConstruct
public void dispatcherInit() {
grantTypeMap.put("红包", resourceId -> grantTypeSerive.redPaper(resourceId));
grantTypeMap.put("购物券", resourceId -> grantTypeSerive.shopping(resourceId));
grantTypeMap.put("vip会员", resourceId -> grantTypeSerive.vip(resourceId));
}
public String getResult(String resourceType, String resourceId) {
// Controller根据 优惠券类型resourceType、编码resourceId 去查询 发放方式grantType
Function<String, String> result = grantTypeMap.get(resourceType);
if (result != null) {
// 传入 resourceId 执行这段表达式获得String型的grantType
return result.apply(resourceId);
}
return "查询不到该优惠券的发放方式";
}
}
@Service
public class GrantTypeSerive {
public String redPaper(String resourceId) {
//红包的发放方式
return "每周末9点发放";
}
public String shopping(String resourceId) {
//购物券的发放方式
return "每周三9点发放";
}
public String vip(String resourceId) {
//qq会员的发放方式
return "每周一0点开始秒杀";
}
}
Map + Java8 函数式接口(解决大量if方法)
最新推荐文章于 2023-03-04 22:14:56 发布