浅谈模式 - 模板方法

在抽象层方法中,定义一些列的行为骨架。并且设计好执行顺序(不变的流程)。具体的行为实现,由子类完成。

几乎任何一个框架,任何系统在抽象层都要使用模板方法。因为框架的骨架由实体域,服务域,回话域三大块组成。服务域代表的就是流程,流程的控制基本都得用上模板方法。

public interface ITemplate {

    void method1();

    void method2();

    void method3();
}
public class TemplateImpl implements ITemplate {

    @Override
    public void method1() {
        System.out.println("method1");
    }

    @Override
    public void method2() {
        System.out.println("method2");
    }

    @Override
    public void method3() {
        System.out.println("method3");
    }
}
public class Client {

    public static void main(String[] args) {
        ITemplate template = new TemplateImpl();

        System.out.println("pre");
        template.method1();
        template.method2();
        template.method3();
        System.out.println("after");
        System.out.println("完成");
    }
}

大家都知道的HttpServlet.service就是模板方法的典型应用,看下。

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) {
            doGet(req, resp);// 调用doGet,就是模板
        } else {
            long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
            if (ifModifiedSince < lastModified) {
                maybeSetLastModified(resp, lastModified);
                doGet(req, resp);// 调用doGet,就是模板
            } else {
                resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            }
        }

    } else if (method.equals(METHOD_HEAD)) {
        long lastModified = getLastModified(req);
        maybeSetLastModified(resp, lastModified);
        doHead(req, resp);// 调用doHead,就是模板

    } else if (method.equals(METHOD_POST)) {
        doPost(req, resp);// 调用doPost,就是模板
        
    } else if (method.equals(METHOD_PUT)) {
        doPut(req, resp);// 调用doPut,就是模板
        
    } else if (method.equals(METHOD_DELETE)) {
        doDelete(req, resp);// 调用doDelete,就是模板
        
    } else if (method.equals(METHOD_OPTIONS)) {
        doOptions(req,resp);// 调用doOptions,就是模板
        
    } else if (method.equals(METHOD_TRACE)) {
        doTrace(req,resp);// 调用doTrace,就是模板
        
    } else {
        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);
    }
}

还有个一个非常典型的案例,Junit

这个地方基本一看就知道是模板方法。

// TestCase.runBare
public void runBare() throws Throwable {
    Throwable exception = null;
    setUp();// 方法1
    try {
        runTest();// 方法2
    } catch (Throwable running) {
        exception = running;
    } finally {
        try {
            tearDown();// 方法3
        } catch (Throwable tearingDown) {
            if (exception == null) exception = tearingDown;
        }
    }
    if (exception != null) throw exception;
}

查看全部 浅谈模式 - 汇总篇

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值