优化前
public class StrategyAndFactoryDemo {
public static void main(String[] args) {
String type = "xxx";
if(QRCodeEnum.one.getCodeType().equalsIgnoreCase(type)){
oneMethod(wxMessgeBean);
}else if(QRCodeEnum.two.getCodeType().equalsIgnoreCase(type)){
twoMethod(wxMessgeBean);
}else if(QRCodeEnum.three.getCodeType().equalsIgnoreCase(type)){
threeMethod(wxMessgeBean);
} else{
log.error("回调:角色类型错误")
}
}
}
public enum QRCodeEnum {
one("one"),
two("two"),
three("three")
;
private final String codeType;
QRCodeEnum(String codeType) {
this.codeType = codeType;
}
public String getCodeType() {
return codeType;
}
}
优化后(方法)
public class StrategyAndFactoryDemo {
public static void main(String[] args) {
List<StrategyFactory<Consumer<WXMessgeBean>>> strategyFactoryList = new ArrayList<>();
String type = "xxx";
strategyFactoryList.add(new StrategyFactory<Consumer<WXMessgeBean>>()
.setCondition(QRCodeEnum.give_product.getCodeType().equalsIgnoreCase(type))
.setContent(this::oneMethod));
strategyFactoryList.add(new StrategyFactory<Consumer<WXMessgeBean>>()
.setCondition(QRCodeEnum.live_link.getCodeType().equalsIgnoreCase(type))
.setContent(this::twoMethod));
strategyFactoryList.add(new StrategyFactory<Consumer<WXMessgeBean>>()
.setCondition(QRCodeEnum.work_qr_code.getCodeType().equalsIgnoreCase(type))
.setContent(this::threeMethod));
List<StrategyFactory<Consumer<WXMessgeBean>>> collect = strategyFactoryList.stream().filter(StrategyFactory::getCondition).collect(Collectors.toList());
if(io.jsonwebtoken.lang.Collections.isEmpty(collect)) log.error("引流关注服务号回调:角色类型错误");
collect.get(0).getContent().accept(wxMessgeBean);
}
}
@Data
@Accessors(chain = true)
public class StrategyFactory<T> {
private Boolean condition;
private T content;
}
public enum QRCodeEnum {
one("one"),
two("two"),
three("three")
;
private final String codeType;
QRCodeEnum(String codeType) {
this.codeType = codeType;
}
public String getCodeType() {
return codeType;
}
}
优化后:(字符串)
public class StrategyAndFactoryDemo {
public static void main(String[] args) {
List<StrategyFactory<String>> strategyFactoryList = new ArrayList<>();
String type = "xxx";
strategyFactoryList.add(new StrategyFactory<String>()
.setCondition(QRCodeEnum.give_product.getCodeType().equalsIgnoreCase(type))
.setContent("测试111"));
strategyFactoryList.add(new StrategyFactory<String>()
.setCondition(QRCodeEnum.live_link.getCodeType().equalsIgnoreCase(type))
.setContent("测试222"));
strategyFactoryList.add(new StrategyFactory<String>()
.setCondition(QRCodeEnum.work_qr_code.getCodeType().equalsIgnoreCase(type))
.setContent("测试333"));
List<StrategyFactory<String>> collect = strategyFactoryList.stream().filter(StrategyFactory::getCondition).collect(Collectors.toList());
if(io.jsonwebtoken.lang.Collections.isEmpty(collect)) System.out.println("条件都不满足");
System.out.println(collect.get(0).getContent());
}
}
@Data
@Accessors(chain = true)
public class StrategyFactory<T> {
private Boolean condition;
private T content;
}
public enum QRCodeEnum {
one("one"),
two("two"),
three("three");
private final String codeType;
QRCodeEnum(String codeType) {
this.codeType = codeType;
}
public String getCodeType() {
return codeType;
}
}