A simple java if statement using polymorphism refactoring

Scenario Description:
In a controller, the department leader has to assign tasks, view the overall situation of the tasks, view the Department members, view the completion of the Department members, and export the task details, as follows:

   @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){
            ...
        }
    }

After a period of time, the new requirements come. The previous Task was a requirement Task. Now there is a test Task with more Task types, but the interface needs to share one set. So a type field is added to a Task to distinguish different tasks. 1: Task of the original type, 2: test Task. The controller becomes the following (Task type should be written in constant):

@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){
				...
			}
		}
	}

After that, new task types are added. Continue to add if else to several methods of the controller. The current development process is controller > Service > Dao. Judge the task, and then go to service to call the business method of the task. Now use the polymorphic method to refactor the if else statement

First, create an abstract class to separate the common methods

        @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);
		
	}    

Then different tasks inherit this abstract class, and the specific business logic is processed on their own classes. Requirements tasks:

        @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){
			...
		}
	}    

Test 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){
			...
		}
	}    

Then create the factory class and return to the task class

  

@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(...){
				...
			}
			...
		}
	}

Finally, the controller is modified and the result is achieved

        @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);
		}
	}    

Tags: Java

Posted on Sat, 02 Nov 2019 05:44:56 -0700 by kristofferlc