欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

策略模式+工厂模式实际应用

程序员文章站 2024-01-20 21:01:16
...

原始场景:
要根据传入的类型(operateType),做各自的初始化处理(为了方便看的更清晰,以下代码做了简化处理)
以下代码在controller层,在这种场景中用了很多if else,看起来就很臃肿吧

@RequestMapping("/toOperReport")
public String toOperReport(String operateType) {
    String resultPath = "";
    if (CaseConstants.BASE_RESOURCE_INFO.equals(operateType)) {
        resultPath = "um/resourceinfo/resourceinfoAddPage";

    } else if (CaseConstants.WAITING_INQUEST.equals(operateType)) {
        resultPath = "um/resourceinfo/resourceInquestReport";
        _inquestrecordService.initData(model, resourceInfo);

    } else if (CaseConstants.WAITING_PUNISH.equals(operateType)) {
        resultPath = "um/resourceinfo/resourcePunishReport";
        _punishService.initData(model, caseId, resourceInfo);

    } else if (CaseConstants.WAITING_SEND_PUNISH_PREINFORM.equals(operateType)) {
        resultPath = "um/proofservice/punishProof";
        _proofserviceService.initData();

    } else if (CaseConstants.WAITING_SEND_HEAR_NOTIFY.equals(operateType)) {
        resultPath = "um/proofservice/punishProof";
        _proofserviceService.initData();

    } else if (CaseConstants.WAITING_SEND_PUNISH.equals(operateType)) {
        resultPath = "um/proofservice/punishProof";
        _proofserviceService.initData();

    } else if (CaseConstants.WAITING_SEND_HEAR_REPORT.equals(operateType)) {
        resultPath = "um/proofservice/punishProof";
        _proofserviceService.initData();

    } else if (CaseConstants.WAITING_FINAL.equals(operateType)) {
        resultPath = "um/resourceinfo/resourcefinalReport";
        _finalreportService.initData();
    }
    return resultPath;
}

改造过程:
1、新建一个策略接口类StrategyInter,用来抽象各业务类需要用到的initData()方法

/**
 * 策略接口
 *
 */
public interface StrategyInter {
     
     /**
      * 初始化各流程页面
      * @return
      */
     public String initData();
     
}

2、新建一个工厂类StrategyFactory,该工厂类是为了在项目启动时调用register()方法,将各业务类型注册到工厂的map中。getPageByOperType()方法是为了获取类型。

public class StrategyFactory {
     private static Map<String, StrategyInter> services = new ConcurrentHashMap<String, StrategyInter>();
     
     public static StrategyInter getPageByOperType(String operateType) {
           return services.get(operateType);
     };
     
     public static void register(String operateType, StrategyInter stratergy) {
           services.put(operateType, stratergy);
     }
}

3、让你的业务实现类,再实现StrategyInter, InitializingBean 这两个接口,StrategyInter接口是为了重写业务类的方法,而实现了InitializingBean接口,就可以在项目启动过程中就执行它的afterPropertiesSet(),从而把业务类型注册到工厂中。

@Override
public void afterPropertiesSet() throws Exception {
    StrategyFactory.register(CaseConstants.WAITING_INQUEST, this);
}
public String initData() {
    ......//这里做各自的业务处理
    return "um/resourceinfo/resourceInquestReport";
}

4、重新写controller里的toOperReport方法

@RequestMapping("/toOperReport")
public String toOperReport() {
    StrategyInter stratergy = StrategyFactory.getPageByOperType(operateType);
    String resultPath = stratergy.initData();
    return resultPath;
}

以上就完成了对之前臃肿代码的改造,通过对比前后controller中的代码,代码是不是变得更加简洁了,而且将来要是有新的类型,直接新添加类实现StrategyInter, InitializingBean就行了,不用再修改之前controller中的代码。