我有三個小服務程序邏輯判斷道呼叫根據登錄的用戶和servlet叫
/Servelt1 (supervisors, managers & president can execute)
/Servelt2 (only managers & president can execute)
/Servelt3 (only president can execute)
每個servlet使用Struts的風格actionMaps。當每個servlet調用與例如 ?method=list
他們都執行Comment.getList()
/actionMap.put("list", new ListAction(modelMap, form, "WEB-INF/views/servlet1_v.jsp"));
/actionMap.put("list", new ListAction(modelMap, form, "WEB-INF/views/servlet2_v.jsp"));
/actionMap.put("list", new ListAction(modelMap, form, "WEB-INF/views/servlet3_v.jsp"));
Class Comment {
//getList called from the 3 servlets
public List<Comment> getList(HttpServletRequest request) {
List<Comment> comments = null;
try {
CommentDetailDAO cdDao = new CommentDetailDAO();
comments = cdDao.getComDetailListForDirectReports(authUser.getBadge());
} catch (DAOException e) {
setError(FORM_RESULTS, e.getMessage());
}
request.setAttribute("comments ", comments);
return comments; //redundant not really used
}
有啥不同道調用基於誰登錄和他們在叫什麼servlet來執行。
if (supervisor) {
comments = cdDao.getComDetailListForDirectReports(authUser.getBadge());
//other dao calls
}
if (manager & servlet1) {
comments = cdDao.getComDetailListForDirectReportsAndTheirDirectReports(authUser.getBadge());
//other dao calls
}
etc...
是使用大量if/then/else邏輯的唯一方法嗎?或者我應該讓每個servlet調用特定的SupervisorCommentClass.getList(),ManagerCommentClass.getList()等。
==編輯 - 在這個週末我對此有所瞭解。由於我有3個不同的Servlet,並且每個servlet有3個不同的視圖,所以最好有3個不同的包含業務邏輯的Class文件。所以不是每個servlet調用Comment.getList()
我想我應該有3個評論類:
SupervisorComment.getList();
ManagerComment.getList();
PresidentComment.getList();
構架哪些是你使用?或者沒有關係? – sndyuk
不使用框架。 – jeff
如果我在案例中編寫代碼,我使用了具有自定義註釋的依賴注入。 – sndyuk