Tomcat

Tomcat中设计模式-门面模式

2018-12-01  本文已影响50人  晴天哥_王志

开篇

 门面模式是对象的结构模式,外部与一个子系统的通信必须通过一个统一的门面对象进行。门面模式提供一个高层次的接口,使得子系统更易于使用,如下图所示(一图胜千言)。

门面模式例子

Tomcat中门面模式的例子

说明:



说明:



说明:



ApplicationContextFacade

说明:

例子源码

说明:

public class RequestFacade implements HttpServletRequest {

    public RequestFacade(Request request) {
        this.request = request;
    }

    protected Request request = null;

    public Object getAttribute(String name) {

        if (request == null) {
            throw new IllegalStateException(
                            sm.getString("requestFacade.nullRequest"));
        }

        return request.getAttribute(name);
    }

    public Enumeration<String> getAttributeNames() {

        if (request == null) {
            throw new IllegalStateException(
                            sm.getString("requestFacade.nullRequest"));
        }

        if (Globals.IS_SECURITY_ENABLED){
            return AccessController.doPrivileged(
                new GetAttributePrivilegedAction());
        } else {
            return request.getAttributeNames();
        }
    }

    public int getContentLength() {

        if (request == null) {
            throw new IllegalStateException(
                            sm.getString("requestFacade.nullRequest"));
        }

        return request.getContentLength();
    }
}



public class Request implements org.apache.catalina.servlet4preview.http.HttpServletRequest {

    private HttpServletRequest applicationRequest = null;

    protected RequestFacade facade = null;

    public HttpServletRequest getRequest() {
        if (facade == null) {
            facade = new RequestFacade(this);
        }
        if (applicationRequest == null) {
            applicationRequest = facade;
        }

        return applicationRequest;
    }
}

参考文章

Tomcat 设计模式总结(Tomcat源代码阅读系列之八)

上一篇 下一篇

猜你喜欢

热点阅读