Tomcat中对外观模式的使用
org.apache.catalina.connector.Request
public class Request
implements HttpServletRequest {
// ...
protected RequestFacade facade = null;
// ...
/**
* Return the <code>ServletRequest</code> for which this object
* is the facade. This method must be implemented by a subclass.
*/
public HttpServletRequest getRequest() {
if (facade == null) {
facade = new RequestFacade(this);
}
return facade;
}
}
org.apache.catalina.connector.RequestFacade
public class RequestFacade implements HttpServletRequest {
// ...
}
Spring中对外观模式的使用
org.springframework.jdbc.support.JdbcUtils
- 在 JdbcUtils 封装了对 java.sql.Connection 的使用;
- JdbcUtils 和 java.sql.Connection 都属于抽象层的依赖关系;
public abstract class JdbcUtils {
// ...
public static void closeConnection(Connection con) {
if (con != null) {
try {
con.close();
}
catch (SQLException ex) {
logger.debug("Could not close JDBC Connection", ex);
}
catch (Throwable ex) {
// We don't trust the JDBC driver: It might throw RuntimeException or Error.
logger.debug("Unexpected exception on closing JDBC Connection", ex);
}
}
}
// ...
}
Mybatis中对外观模式的使用
- Configuration 作为外观类对外提供接口;
- Configuration 中关联了很多其他的类(子系统),Configuration 的接口对这些类的功能进行封装,对外屏蔽对这些成员的组合调用;
public class Configuration {
// ...
protected ObjectFactory objectFactory = new DefaultObjectFactory();
protected ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();
protected ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
// ...
public MetaObject newMetaObject(Object object) {
return MetaObject.forObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
}
}