【设计模式】策略模式+工厂模式实际应用

原始场景:
要根据传入的类型(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中的代码。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值