Tomcat中设计模式-门面模式

简介:

开篇

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

门面模式例子


Tomcat中门面模式的例子

说明:

  • RequestFacade作为Request的门面,内部包含Request对象。



说明:

  • ResponseFacade作为Response的门面,内部包含Response对象。



说明:

  • StandardSessionFacade作为HttpSession的门面,内部包含HttpSession对象。



ApplicationContextFacade

说明:

  • ApplicationContextFacade作为ApplicationContext的门面,内部包含ApplicaitonContext对象。


例子源码

说明:

  • RequestFacade内部包含Request对象。
  • 对于Request对象的访问通过RequestFacade进行访问。
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源代码阅读系列之八)

目录
相关文章
|
21天前
|
设计模式 安全 Java
【分布式技术专题】「Tomcat技术专题」 探索Tomcat技术架构设计模式的奥秘(Server和Service组件原理分析)
【分布式技术专题】「Tomcat技术专题」 探索Tomcat技术架构设计模式的奥秘(Server和Service组件原理分析)
23 0
|
6月前
|
设计模式 算法
设计模式7 - 门面模式【Facade Pattern】
设计模式7 - 门面模式【Facade Pattern】
27 1
|
6月前
|
设计模式 算法 C++
设计模式之门面模式(C++)
设计模式之门面模式(C++)
|
3月前
|
设计模式 Java 应用服务中间件
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
30 1
|
3月前
|
设计模式
设计模式 | 门面模式 Facade
设计模式 | 门面模式 Facade
18 0
|
3月前
|
设计模式 传感器
将设计模式门面模式运用到生活当中
将设计模式门面模式运用到生活当中
|
5月前
|
设计模式
设计模式系列教程(10) - 外观模式(门面模式)
设计模式系列教程(10) - 外观模式(门面模式)
67 0
|
6月前
|
设计模式 Java
设计模式~门面(外观)模式(Facade)-08
目录 (1)优点 (2)缺点 (3)使用场景 (4)注意事项: (5)应用实例: (6)源码中的经典应用 代码 外观模式(Fac
26 0
|
6月前
|
设计模式
设计模式~门面模式-05
门面模式 优点 缺点 使用场景
22 0
|
9月前
|
设计模式
设计模式 | 门面模式 Facade
设计模式 | 门面模式 Facade
50 0