一般在做逻辑判断的时候我们一般难免会写出以下的代码,以电商的会员制度为例
if (type == "AVIP") {
System.out.println("A output");
} else if (type == "BVIP") {
System.out.println("B output");
} else if (type == "CVIP") {
System.out.println("C output");
} else {
System.out.println("output");
}}
- 1对于以上的代码要优化的化很容易就想到了策略模式来做优化,首先定义一个策略接口
public interface StrategyInf {
void comput(BigDecimal money);
String getType();
}
- 2具体策略的实现
publi class CommonStrategy implements StrategyInf {
@Override
public void comput(BigDecimal money) {
Optional.ofNullable(money).ifPresent(m -> {
System.out.println(m.multiply(new BigDecimal(VIPEnums.COMMON_VIP.getSaleCount())));
});
}
@Override
public String getType() {
return VIPEnums.COMMON_VIP.getType();
}
}
public static class SilverStrategy implements StrategyInf {
@Override
public void comput(BigDecimal money) {
Optional.ofNullable(money).ifPresent(m -> {
System.out.println(m.multiply(new BigDecimal(VIPEnums.SLIVER_VIP.getSaleCount())));
});
}
@Override
public String getType() {
return VIPEnums.SLIVER_VIP.getType();
}
}
public class GoldStrategy implements StrategyInf {
@Override
public void comput(BigDecimal money) {
Optional.ofNullable(money).ifPresent(m -> {
System.out.println(m.multiply(new BigDecimal(VIPEnums.GOLD_VIP.getSaleCount())));
});
}
@Override
public String getType() {
return VIPEnums.GOLD_VIP.getType();
}
}
public class PlatinumStrategy implements StrategyInf {
@Override
public void comput(BigDecimal money) {
Optional.ofNullable(money).ifPresent(m -> {
System.out.println(m.multiply(new BigDecimal(VIPEnums.PLATINUM_VIP.getSaleCount())));
});
}
@Override
public String getType() {
return VIPEnums.PLATINUM_VIP.getType();
}
}
这个时候发现最开始的代码就做了一层封装,变成了如下
if (type == AVIP) {
new AStrategy().comput();
} else if (type == BVIP) {
new BStrategy().comput();
} else if (type == CVIP){
new CStrategy().comput();
} else {
.....
}
以上的代码确实看起来好些了,但是进一步优化可以把IF-ELSE省略, 工厂+策略模式
public static class StrategyFactory {
private Map<String,StrategyInf> stratgyMap;
private volatile static StrategyFactory INSTANCE = null;
private StrategyFactory() {
List<StrategyInf> strategies = new ArrayList<>();
strategies.add(new CommonStrategy());
strategies.add(new SilverStrategy());
strategies.add(new GoldStrategy());
strategies.add(new PlatinumStrategy());
stratgyMap = strategies.stream()
.collect(Collectors.toMap(StrategyInf::getType,strategy -> strategy));
}
public static StrategyFactory getInstance() {
synchronized (StrategyFactory.class) {
if (INSTANCE == null) {
INSTANCE = new StrategyFactory();
}
}
return INSTANCE;
}
public StrategyInf getStrategy(String type) {
return stratgyMap.get(type);
}
}
最终只要一行代码就能达到最开始的效果
public static void main(String[] args) {
StrategyFactory.getInstance().get(type).comput();
}
还有一种枚举优化如下
public enum Logistics
{
SF("顺丰速运","SF"),
EMS("EMS标准快递","EMS"),
EYB("EMS经济快件","EYB"),
ZJS("宅急送","ZJS");
Logistics(String logisticsName, String logisticsCode)
{
this.logisticsName = logisticsName;
this.logisticsCode = logisticsCode;
}
private String logisticsName;
private String logisticsCode;
public String getLogisticsName() {
return logisticsName;
}
public static String getLogisticsCodeByName(String logisticsName) {
for (Logistics logistics : Logistics.values()) {
if (logistics.getLogisticsName().equals(logisticsName)
|| (StringUtils.isNotEmpty(logisticsName) && logisticsName.indexOf(logistics.getLogisticsName()) >= 0)) {
return logistics.getLogisticsCode();
}
}
return null;
}
public static String getLogisticsNameByCode(String logisticsCode) {
for (Logistics logistics : Logistics.values()) {
if (logistics.getLogisticsCode().equals(logisticsCode)) {
return logistics.getLogisticsName();
}
}
return null;
}
public void setLogisticsName(String logisticsName) {
this.logisticsName = logisticsName;
}
public String getLogisticsCode() {
return logisticsCode;
}
public void setLogisticsCode(String logisticsCode) {
this.logisticsCode = logisticsCode;
}
}
//用枚举干掉if else
String compCode = TradePrintLogForExport.Logistics.getLogisticsCodeByName(campanyName);
//普通
if (campaynyName == "顺丰") {
return "SF";
} else if (campanyName == "宅急送") {
return "ZJS";
} else if (campanyName == "EMS标准快递") {
return "EMS";
} else ......