记一次java简单的if语句使用多态重构
场景描述:
一个controller中,部门领导有布置任务,查看任务整体情况,查看部门成员,查看部门成员完成情况,导出任务详情,如下:
@RestController @RequestMapping(value = "/task") public class TaskController{ @PostMapping(value = "/saveTask") public JSONObject saveTask(Task task){ ... } @PostMapping(value = "/taskDetail") public JSONObject taskDetail(Task task){ ... } @PostMapping(value = "/memberList") public JSONObject memberList(Task task){ ... } @PostMapping(value = "/memberDetail") public JSONObject memberDetail(Task task){ ... } @PostMapping(value = "/exportTask") public JSONObject exportTask(Task task){ ... } }
过了一段时间后,新需求来了,之前任务是需求任务,现在任务种类多了一个测试任务,但是接口要公用一套。所以Task加了一个type的字段用来区分不同的任务,1:原来类型的任务,2:测试任务,controller变成如下(Task的类型type应该写在常量里面):
@RestController @RequestMapping(value = "/task") public class TaskController{ @PostMapping(value = "/saveTask") public JSONObject saveTask(Task task){ if( Constants.COMMON_TASK == task.type){ ... }else if( Constants.TEST_TASK == task.type){ ... } } @PostMapping(value = "/taskDetail") public JSONObject taskDetail(Task task){ if( Constants.COMMON_TASK == task.type){ ... }else if( Constants.TEST_TASK == task.type){ ... } } @PostMapping(value = "/memberList") public JSONObject memberList(Task task){ if( Constants.COMMON_TASK == task.type){ ... }else if( Constants.TEST_TASK == task.type){ ... } } @PostMapping(value = "/memberDetail") public JSONObject memberDetail(Task task){ if( Constants.COMMON_TASK == task.type){ ... }else if( Constants.TEST_TASK == task.type){ ... } } @PostMapping(value = "/exportTask") public JSONObject exportTask(Task task){ if( Constants.COMMON_TASK == task.type){ ... }else if( Constants.TEST_TASK == task.type){ ... } } }
再之后又有新任务类型加进来,继续往这个controller的几个方法中增加if-else。目前的开发流程是controller–>service–>dao,判断那种任务,然后走到service调用这个任务的业务方法,现在使用多态的方法把if-else语句重构掉
首先创建一个抽象类,把共同的方法抽离出来
@Service public abstract class BaseTaskService{ public abstract JSONObject saveTask(Task task); public abstract JSONObject taskDetail(Task task); public abstract JSONObject memberList(Task task); public abstract JSONObject memberDetail(Task task); public abstract JSONObject exportTask(Task task); }
然后不同的任务继承这个抽象类,具体的业务逻辑在自己的类上处理,需求任务:
@Service public class CommonTask extends BaseTaskService{ @Ovreride public JSONObject saveTask(Task task){ ... } @Ovreride public JSONObject taskDetail(Task task){ ... } @Ovreride public JSONObject memberList(Task task){ ... } @Ovreride public JSONObject memberDetail(Task task){ ... } @Ovreride public JSONObject exportTask(Task task){ ... } }
测试任务:
@Service public class TestTask extends BaseTaskService{ @Ovreride public JSONObject saveTask(Task task){ ... } @Ovreride public JSONObject taskDetail(Task task){ ... } @Ovreride public JSONObject memberList(Task task){ ... } @Ovreride public JSONObject memberDetail(Task task){ ... } @Ovreride public JSONObject exportTask(Task task){ ... } }
再之后创建工厂类返回任务类
@Service public class TaskFactory{ @Autowired private CommonTask commonTask; @Autowired private TestTask testTask; ... public BaseTaskService getTask(Integer type){ if(Constants.COMMON_TASK.equals(type){ return commonTask; }else if(Constants.TestTask.equals(type){ return testTask; }else if(...){ ... } ... } }
最后修改controller就大功告成
@RestController @RequestMapping(value = "/task") public class TaskController{ @Autowired private TaskFactory taskFactory; @PostMapping(value = "/saveTask") public JSONObject saveTask(Task task){ BaseTaskService currentTask = taskFactory.getTask(task); return currentTask.saveTask(task); } @PostMapping(value = "/taskDetail") public JSONObject taskDetail(Task task){ BaseTaskService currentTask = taskFactory.getTask(task); return currentTask.taskDetail(task); } @PostMapping(value = "/memberList") public JSONObject memberList(Task task){ BaseTaskService currentTask = taskFactory.getTask(task); return currentTask.memberList(task); } @PostMapping(value = "/memberDetail") public JSONObject memberDetail(Task task){ BaseTaskService currentTask = taskFactory.getTask(task); return currentTask.memberDetail(task); } @PostMapping(value = "/exportTask") public JSONObject exportTask(Task task){ BaseTaskService currentTask = taskFactory.getTask(task); return exportTask(task); } }