设计模式之模板设计模式

解决问题

让父类控制子类方法的调用顺序,或者执行时机

模板设计模式好处:

  1. 开发人员在开发时,只需要考虑方法的实现。
  2. 不需要考虑方法在何种情况下被调用
  3. 参看HttpServlet的server方法

示例代码

/**
 * 在lifetimePlanning方法中,规定了其中几个方法的调用顺序
 * 当子类调用该方法时
 * 若子类重写了该方法,那么则调用子类的方法,但是执行顺序不变,或者执行时机不变
 * 用final修饰,子类不可重写,缺少了拓展性,也可不用final修饰
 */
public class Father {

    public void life(){
        System.out.println("父亲的生活");
    }

    public void work(){
        System.out.println("父亲的工作");
    }

    public void study(){
        System.out.println("父亲的学习");
    }

    public final void lifetimePlanning(){
        this.study();
        this.work();
        this.life();
    }
}

public class Son extends Father {
    @Override
    public void life() {
        System.out.println("儿子的生活");
    }

    @Override
    public void work() {
        System.out.println("儿子的工作");
    }

    @Override
    public void study() {
        System.out.println("儿子的学习");
    }

    public static void main(String[] args) {
        Son son = new Son();
        son.lifetimePlanning();
    }
}

设计模式之模板设计模式_第1张图片

参看Servlet的Server方法

有请求已经Servlet则会第一时间调用service方法。但是我们只需要关注doGet或者doPost方法实现即可。其实HttpServlet正是使用了模板的设计模式,把判断是get请求,还是post请求做完了。当程序判断完请求类型,则会调用相应的doGet,doPost

public class Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    }
}
public abstract class HttpServlet extends GenericServlet {
    //....
    protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        String method = req.getMethod();

        if (method.equals(METHOD_GET)) {
            long lastModified = getLastModified(req);
            if (lastModified == -1) {
                // servlet doesn't support if-modified-since, no reason
                // to go through further expensive logic
                doGet(req, resp);
            } else {
                long ifModifiedSince;
                try {
                    ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
                } catch (IllegalArgumentException iae) {
                    // Invalid date header - proceed as if none was set
                    ifModifiedSince = -1;
                }
                if (ifModifiedSince < (lastModified / 1000 * 1000)) {
                    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                } else {
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                }
            }

        } else if (method.equals(METHOD_HEAD)) {
            long lastModified = getLastModified(req);
            maybeSetLastModified(resp, lastModified);
            doHead(req, resp);

        } else if (method.equals(METHOD_POST)) {
            doPost(req, resp);

        } else if (method.equals(METHOD_PUT)) {
            doPut(req, resp);

        } else if (method.equals(METHOD_DELETE)) {
            doDelete(req, resp);

        } else if (method.equals(METHOD_OPTIONS)) {
            doOptions(req,resp);

        } else if (method.equals(METHOD_TRACE)) {
            doTrace(req,resp);

        } else {
            //
            // Note that this means NO servlet supports whatever
            // method was requested, anywhere on this server.
            //

            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[1];
            errArgs[0] = method;
            errMsg = MessageFormat.format(errMsg, errArgs);

            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
        }
    }
    //....
}

你可能感兴趣的:(设计模式,设计模式)