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

Tomcat中门面模式的例子

说明:
- RequestFacade作为Request的门面,内部包含Request对象。

说明:
- ResponseFacade作为Response的门面,内部包含Response对象。

说明:
- StandardSessionFacade作为HttpSession的门面,内部包含HttpSession对象。

说明:
- ApplicationContextFacade作为ApplicationContext的门面,内部包含ApplicaitonContext对象。
例子源码
说明:
- 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;
}
}
网友评论