看看使用策略模式之前的代码:
java
// switch (type) {
// case 0:
// terme.setKhdmlist(dmList);
// List<Kehu> list = getSdList$api(terme);
// for (Kehu kehu : list) {
// map = new HashMap<String, String>();
// map.put(kehu.getKhdm(), kehu.getKhmc());
// mapList.add(map);
// }
// break;
// case 1:
// terme.setGhsdmlist(dmList);
// List<Gonghuoshang> list1 = getGhsList$api(terme);
// for (Gonghuoshang gonghuoshang : list1) {
// map = new HashMap<String, String>();
// map.put(gonghuoshang.getGhsdm(), gonghuoshang.getGhsmc());
// mapList.add(map);
// }
// break;
// case 2:
// terme.setKhdmlist(dmList);
// List<Kehu> list2 = getKehuList$api(terme);
// for (Kehu kehu : list2) {
// map = new HashMap<String, String>();
// map.put(kehu.getKhdm(), kehu.getKhmc());
// mapList.add(map);
// }
// break;
// case 3:
// terme.setCkdmlist(dmList);
// List<Cangku> list3 = getCangkuList$api(terme);
// for (Cangku cangku : list3) {
// map = new HashMap<String, String>();
// map.put(cangku.getCkdm(), cangku.getCkmc());
// mapList.add(map);
// }
// break;
// case 4:
// terme.setQddmlist(dmList);
// List<Qudao> list4 = getQudaoList$api(terme);
// for (Qudao qudao : list4) {
// map = new HashMap<String, String>();
// map.put(qudao.getQddm(), qudao.getQdmc());
// mapList.add(map);
// }
// break;
// case 5:
// terme.setQydmlist(dmList);
// List<Quyu> list5 = getQuyuList$api(terme);
// for (Quyu quyu : list5) {
// map = new HashMap<String, String>();
// map.put(quyu.getQydm(), quyu.getQymc());
// mapList.add(map);
// }
// break;
// case 6:
// terme.setPpdmlist(dmList);
// List<Pinpai> list6 = getPinpaiList$api(terme);
// for (Pinpai pinpai : list6) {
// map = new HashMap<String, String>();
// map.put(pinpai.getPpdm(), pinpai.getPpmc());
// mapList.add(map);
// }
// break;
// default:
// break;
// }
// for (SysLimitItemGroupmx itemGroupmx : resList) {
// boolean flag = false;
// if (isNotEmpty(mapList)) {
// for (Map<String, String> map1 : mapList) {
// if (map1.containsKey(itemGroupmx.getDxdm())) {
// flag = true;
// if (!map1.containsValue(itemGroupmx.getDxmc())) {
// itemGroupmx.setBz(str + "编码对应的" + str + "名称不对!");
// }
// break;
// }
// }
// }
// if (!flag) {
// itemGroupmx.setBz(str + "编码对应的数据不存在!");
// }
// }
使用 策略模式 之后的代码:
枚举类:
java
public enum LimitItemTypeEnum {
OUTMETHOD(-1 ,"-1", "输出方法"),
SHOP (0 ,"0", "XX"),
SUPPLIER(1 ,"1", "XXX"),
CUSTOMER(2 ,"2", "XX"),
STORE (3 ,"3", "XX"),
CHANNEL (4 ,"4", "XX"),
AREA (5, "5", "XX"),
BRAND (6, "6", "XX"),
;
private final Integer number;
private final String code;
private final String info;
LimitItemTypeEnum(Integer number, String code, String info) {
this.number=number;
this.code = code;
this.info = info;
}
public String getCode() {
return code;
}
public String getInfo() {
return info;
}
public Integer getNumber() {
return number;
}
public String getInfoByNumber(int numberIn){
String infoOut="";
for(LimitItemTypeEnum typeEnum:LimitItemTypeEnum.values()){
if(typeEnum.getNumber()==numberIn){
infoOut=typeEnum.getInfo();
break;
}
}
return infoOut;
}
}
接口:
java
public interface CheckListItemService {
void check(Long tenantId,List<SysLimitItemGroupmx> mxList) throws Exception;
}
实现类:
java
public class CheckAreaListItemServiceImpl extends ApiFrag implements CheckListItemService {
@Override
public void check(Long tenantId,List<SysLimitItemGroupmx> mxList) throws Exception{
String typeName= LimitItemTypeEnum.AREA.getInfo();
Terme term=new Terme();
term.setTenantId(tenantId);
term.setQydmlist(listField2StrList(mxList,"dxdm"));
List<Quyu> list = getQuyuList$api(term);
Map<String, String> map = new HashMap();;
for (Quyu quyu : list) {
map.put(quyu.getQydm(), quyu.getQymc());
}
for (SysLimitItemGroupmx itemGroupmx : mxList) {
String key=itemGroupmx.getDxdm();
boolean flag = map.containsKey(key);
if(flag) itemGroupmx.setDxmc(map.get(key));
else itemGroupmx.setBz(typeName + "编码对应的数据不存在!");
itemGroupmx.setFlag(flag);
}
}
}
java
public class CheckBrandListItemServiceImpl extends ApiFrag implements CheckListItemService {
@Override
public void check(Long tenantId,List<SysLimitItemGroupmx> mxList) throws Exception{
String typeName= LimitItemTypeEnum.BRAND.getInfo();
Terme term=new Terme();
term.setTenantId(tenantId);
term.setPpdmlist(listField2StrList(mxList,"dxdm"));
List<Pinpai> list = getPinpaiList$api(term);
Map<String, String> map = new HashMap();;
for (Pinpai item : list) {
map.put(item.getPpdm(), item.getPpmc());
}
for (SysLimitItemGroupmx itemGroupmx : mxList) {
String key=itemGroupmx.getDxdm();
boolean flag = map.containsKey(key);
if(flag) itemGroupmx.setDxmc(map.get(key));
else itemGroupmx.setBz(typeName + "编码对应的数据不存在!");
itemGroupmx.setFlag(flag);
}
}
}
上下文类:
java
public class CheckLimitItemProcessor {
private final Map<Integer, CheckListItemService> strategyMap;
public CheckLimitItemProcessor() {
strategyMap = new HashMap<>();
strategyMap.put(LimitItemTypeEnum.AREA.getNumber(), new CheckAreaListItemServiceImpl());
strategyMap.put(LimitItemTypeEnum.BRAND.getNumber(), new CheckBrandListItemServiceImpl());
strategyMap.put(LimitItemTypeEnum.CUSTOMER.getNumber(), new CheckCustomerListItemServiceImpl());
strategyMap.put(LimitItemTypeEnum.SHOP.getNumber(), new CheckShopListItemServiceImpl());
strategyMap.put(LimitItemTypeEnum.STORE.getNumber(), new CheckStoreListItemServiceImpl());
strategyMap.put(LimitItemTypeEnum.SUPPLIER.getNumber(), new CheckSupplierListItemServiceImpl());
}
public void checkItem(Integer type,Long tenantId, List<SysLimitItemGroupmx> mxList)
throws Exception{
CheckListItemService strategy = strategyMap.get(type);
if (strategy == null) {
throw new IllegalArgumentException("Unsupported LimitItem type.");
}
strategy.check(tenantId,mxList);
}
}
使用:
java
CheckLimitItemProcessor processor = new CheckLimitItemProcessor();
processor.checkItem(type,tenantId,resList);
在java 中实际开发功能时候,我想绝大多数人应该是倾向于使用case when 的,而设计模式大部分用于架构层,至于该怎么用,我觉得还是黑猫白猫都行。