zoukankan      html  css  js  c++  java
  • 设计模式课程 设计模式精讲 10-3 外观模式源码解析

    1    源码解析

    1.1  源码解析1(jdk中的JDBCUtils工具类)

    1.2  源码解析2(mybaties应用的Configuration)

    1.3  源码解析3(在tomcat(7.0.9版本或以上)中的应用)

     

    1    源码解析
    1.1  源码解析1(jdk中的JDBCUtils工具类)

    jdbc在springJDBC中的封装

    /**
         * Close the given JDBC Connection and ignore any thrown exception.
         * This is useful for typical finally blocks in manual JDBC code.
         * @param con the JDBC Connection to close (may be {@code null})
         */
        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);
                }
            }
        }
    
        /**
         * Close the given JDBC Statement and ignore any thrown exception.
         * This is useful for typical finally blocks in manual JDBC code.
         * @param stmt the JDBC Statement to close (may be {@code null})
         */
        public static void closeStatement(Statement stmt) {
            if (stmt != null) {
                try {
                    stmt.close();
                }
                catch (SQLException ex) {
                    logger.trace("Could not close JDBC Statement", ex);
                }
                catch (Throwable ex) {
                    // We don't trust the JDBC driver: It might throw RuntimeException or Error.
                    logger.trace("Unexpected exception on closing JDBC Statement", ex);
                }
            }
        }
    
        /**
         * Close the given JDBC ResultSet and ignore any thrown exception.
         * This is useful for typical finally blocks in manual JDBC code.
         * @param rs the JDBC ResultSet to close (may be {@code null})
         */
        public static void closeResultSet(ResultSet rs) {
            if (rs != null) {
                try {
                    rs.close();
                }
                catch (SQLException ex) {
                    logger.trace("Could not close JDBC ResultSet", ex);
                }
                catch (Throwable ex) {
                    // We don't trust the JDBC driver: It might throw RuntimeException or Error.
                    logger.trace("Unexpected exception on closing JDBC ResultSet", ex);
                }
            }
        }
    1.2  源码解析2(mybaties应用的Configuration)

    通过封装之后,我们的客户端都有这些功能,这一组接口交给客户端来访问

    public MetaObject newMetaObject(Object object) {
        return MetaObject.forObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
      }
    
      public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
        ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
        parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
        return parameterHandler;
      }
    
      public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,
          ResultHandler resultHandler, BoundSql boundSql) {
        ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
        resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
        return resultSetHandler;
      }
    1.3  源码解析3(在tomcat(7.0.9版本或以上)中的应用)

    request.java 和RequestFacade.java 都是HttpServletRequest  的子类,request.java声明RequestFacade,获取request的时候,实际返回的是RequestFacade的对象。我们看起来是使用了HTTPRequest的类,实际上我们使用的是RequestFacade这个外观类。

    RequestFacade

    @SuppressWarnings("deprecation")
    public class RequestFacade implements HttpServletRequest {
    
        @Override
        public String getParameter(String name) {
    
            if (request == null) {
                throw new IllegalStateException(
                                sm.getString("requestFacade.nullRequest"));
            }
    
            if (Globals.IS_SECURITY_ENABLED){
                return AccessController.doPrivileged(
                    new GetParameterPrivilegedAction(name));
            } else {
                return request.getParameter(name);
            }
        }
    
    }

    request.java

    public class Request
    implements HttpServletRequest {
    
    /**
         * The facade associated with this request.
         */
        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;
        }
    
    }
  • 相关阅读:
    自然语言处理一些读书笔记和自己的思考。
    文本情感分析的基础在于自然语言处理、情感词典、机器学习方法等内容。以下是我总结的一些资源。
    自然语言处理哪家强?
    2016,2017中国高考状元调查报告 教师公务员家庭最盛产状元
    书籍装帧知识: 封面 封里 封底 书脊 书冠 书脚 扉页 插页 篇章页目录 版权页 索引 版式 版心 版口 超版口 直(竖)排本 横排本 刊头 破栏 天头 地脚 暗页码 页 另页起 另面起 表注 图注 背题
    How to intercept any postback in a page?
    HearthBuddy卡组
    Button.OnClientClick
    Async Task Types in C#
    ILSpy C# language support status
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/11484213.html
Copyright © 2011-2022 走看看