美文网首页Tomcat
Tomcat中设计模式-门面模式

Tomcat中设计模式-门面模式

作者: 晴天哥_王志 | 来源:发表于2018-12-01 11:29 被阅读50次

开篇

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

门面模式例子

Tomcat中门面模式的例子

说明:

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



说明:

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



说明:

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



ApplicationContextFacade

说明:

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

例子源码

说明:

    1. RequestFacade内部包含Request对象。
  • 2.对于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源代码阅读系列之八)

相关文章

  • Tomcat中设计模式-门面模式

    开篇  门面模式是对象的结构模式,外部与一个子系统的通信必须通过一个统一的门面对象进行。门面模式提供一个高层次的接...

  • Tomcat中设计模式

    1 Tomcat中设计模式 在Tomcat中用了很多设计模式,如模板模式、工厂模式和单例模式等一些常用的设计模式,...

  • iOS单例模式

    1 单例模式 它是一种设计模式(常见的设计模式有:观察者模式、工厂模式、门面模式等)。单例设计模式中,一个类只有一...

  • 设计模式-门面模式

    一:门面模式的定义 外观模式的目的不是给予子系统添加新的功能接口,而是为了让外部减少与子系统内多个模块的交互,松散...

  • 设计模式—门面模式

    门面(Facade)模式的定义:是一种通过为多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式...

  • 外观模式(Facede)

    本文参考自: 《JAVA设计模式》之外观模式(Facade) 1. 作用 外观模式也叫门面模式,门面模式是对象的结...

  • OkHttp的使用之设计模式

    使用的设计模式有: 创建者模式 策略模式 门面模式 责任链模式

  • 门面模式设计

    门面模式 两个作用: 1、简化类的接口 2、消除类与使用它的客户代码之间的耦合 门面模式常常是开发人员最亲密的朋友...

  • 设计模式——门面模式(外观模式)

    《Head First 设计模式》《设计模式之禅(第二版)》 学习笔记,码云同步更新中如有错误或不足之处,请一定指...

  • 学好设计模式防被祭天:门面模式

    为了防止被“杀”了祭天,学点设计模式,并总结下还是有必要的。 一:理解 门面模式也称为外观模式。 门面模式提供了一...

网友评论

    本文标题:Tomcat中设计模式-门面模式

    本文链接:https://www.haomeiwen.com/subject/pnelcqtx.html