Tôi đang cố gắng viết một servlet thực hiện nhiệm vụ dựa trên giá trị "hành động" được chuyển thành đầu vào.
Đây là mẫu trong đó
public class SampleClass extends HttpServlet {
public static void action1() throws Exception{
//Do some actions
}
public static void action2() throws Exception{
//Do some actions
}
//And goes on till action9
public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {
String action = req.getParameter("action");
/**
* I find it difficult in the following ways
* 1. Too lengthy - was not comfortable to read
* 2. Makes me fear that action1 would run quicker as it was in the top
* and action9 would run with a bit delay - as it would cross check with all the above if & else if conditions
*/
if("action1".equals(action)) {
//do some 10 lines of action
} else if("action2".equals(action)) {
//do some action
} else if("action3".equals(action)) {
//do some action
} else if("action4".equals(action)) {
//do some action
} else if("action5".equals(action)) {
//do some action
} else if("action6".equals(action)) {
//do some action
} else if("action7".equals(action)) {
//do some action
} else if("action8".equals(action)) {
//do some action
} else if("action9".equals(action)) {
//do some action
}
/**
* So, the next approach i tried it with switch
* 1. Added each action as method and called those methods from the swith case statements
*/
switch(action) {
case "action1": action1();
break;
case "action2": action2();
break;
case "action3": action3();
break;
case "action4": action4();
break;
case "action5": action5();
break;
case "action6": action6();
break;
case "action7": action7();
break;
case "action8": action8();
break;
case "action9": action9();
break;
default:
break;
}
/**
* Still was not comfortable since i am doing un-necessary checks in one way or the other
* So tried with [reflection][1] by invoking the action methods
*/
Map<String, Method> methodMap = new HashMap<String, Method>();
methodMap.put("action1", SampleClass.class.getMethod("action1"));
methodMap.put("action2", SampleClass.class.getMethod("action2"));
methodMap.get(action).invoke(null);
/**
* But i am afraid of the following things while using reflection
* 1. One is Security (Could any variable or methods despite its access specifier) - is reflection advised to use here?
* 2. Reflection takes too much time than simple if else
*/
}
}
Tất cả những gì tôi cần là thoát khỏi quá nhiều if / other-if kiểm tra mã của tôi để dễ đọc hơn và bảo trì mã tốt hơn. Vì vậy, đã cố gắng cho các lựa chọn thay thế khác như
1. trường hợp chuyển đổi - vẫn còn quá nhiều kiểm tra trước khi thực hiện hành động của tôi
2. phản ánh
i] một điều chính là bảo mật - cho phép tôi truy cập ngay cả các biến và phương thức trong lớp mặc dù chỉ định truy cập của nó - tôi không chắc chắn thời tiết tôi có thể sử dụng nó trong mã của mình
ii] và cái khác là mất nhiều thời gian hơn so với kiểm tra if / other-if đơn giản
Có cách tiếp cận nào tốt hơn hoặc thiết kế tốt hơn mà ai đó có thể đề xuất để tổ chức mã trên theo cách tốt hơn không?
EDITED
Tôi đã thêm câu trả lời cho đoạn trích trên xem xét câu trả lời dưới đây .
Tuy nhiên, các lớp sau "ExecutorA" và "ExecutorB" chỉ thực hiện một vài dòng mã. Có phải là một thực hành tốt để thêm chúng như một lớp hơn là thêm nó như một phương thức? Xin tư vấn về vấn đề này.