模板方法模式應用

GOF給模板方法(Template Method)模式定義一個操作中的算法的骨架,而將一些步驟延遲到子類中。使得子類可以不改變一個算法的結構即可重定義該算法的某些特定步驟。這裏的算 法的結構,可以理解爲你根據需求設計出來的業務流程。特定的步驟就是指那些可能在內容上存在變數的環節。

可以看出來,模板方法模式也是爲了巧妙解決變化對系統帶來的影響而設計的。使用模板方法使系統擴展性增強,最小化了變化對系統的影響。這一點,在下面的舉例中可以很明顯的看出來。

來看下這個簡單模式的結構吧:

1) AbstractClass(抽象類):定義了一到多個的抽象方法,以供具體的子類來實現它們;而且還要實現一個模板方法,來定義一個算法的骨架。該模板方法不僅調用前面的抽象方法,也可以調用其他的操作,只要能完成自身的使命。

2) ConcreteClass(具體類):實現父類中的抽象方法以完成算法中與特定子類相關的步驟。

最近項目中網頁超時判斷、權限控制等功能,使用模板方法模式在控制層實現,頗有借鑑意義。

public abstract class BaseAction extends Action {
    private static Logger logger = Logger.getLogger(BaseAction.class);

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        logger.info("now enter BaseAction:execute method!");
        HttpSession session = request.getSession(false);
        ActionErrors errors = new ActionErrors();
        logger.info("session:" + session);
        // 網頁超時
        if (session == null) {
            logger.error("session has been timeout!");
            request.setAttribute("message", "網頁超時,請重新登錄!");
            return mapping.findForward("session_time_out");
        } else {
            if ((OperatorInfoView) session.getAttribute("operator") == null) {
                logger.info(" OperatorInfoView is null");
                request.setAttribute("message", "網頁超時,請重新登錄!");
                return mapping.findForward("session_time_out");
            }
        }

        // 權限
        Boolean flag = Boolean.FALSE;
        String authorityKey = this.getAuthorityId();
        flag = this.getPrivilege(session, authorityKey);
        if (!flag.booleanValue()) {
            request.setAttribute("message", "對不起,您沒有權限!");
            return mapping.findForward("error");
        }
        logger.info("BaseAction execute method operated successfully!");
        return execute(mapping, form, request, response, errors, session);
    }

    public abstract ActionForward execute(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors,
            HttpSession session);

    /**
     * 獲得權限
     */
    public abstract String getAuthorityId();

    protected Boolean getPrivilege(HttpSession session, String authorityKey) {
        Boolean flag = Boolean.FALSE;
        List authorityList = (List) session.getAttribute("authorityList");
        if (authorityList != null) {
           ...
           flag = Boolean.TRUE;
        }
        return flag;
    }
    //打印錯誤信息
    public void messageError(HttpServletRequest request, String msg) {
		    ActionMessages actionMessages = new ActionMessages();
		    actionMessages.add(Constant.ERROR, new ActionMessage(msg));
		    addMessages(request, actionMessages);
    }
}

 

public class PreAddPersonAction extends BaseAction {
	private static final Logger logger = Logger.getLogger(PreAddPersonAction.class);
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			ActionErrors errors, HttpSession session) {
			messageError(request, "資源文件信息");
			return mapping.findForward("success");
	}

	@Override
	public String getAuthorityId() {
		return 常量;
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章