C03 抽象工厂 JDK源码分析

java.sql.Connection

  • 接口java.sql.Connection相当于抽象工厂CourseFactory,定义了一个产品族会生产哪些产品;
  • 抽象方法的返回值java.sql.Statement和java.sql.PreparedStatement相当于定义了2个产品等级结构,对应Video和Article;
  • java.sql.Statement和java.sql.PreparedStatement也是接口;
  • com.mysql.jdbc.ConnectionImpl是一个具体工厂,生产产品族MySQL的产品,其中生产的com.mysql.jdbc.StatementImpl和com.mysql.jdbc.ServerPreparedStatement是具体产品,都属于MySQL产品族;

抽象工厂

  • java.sql.Connection;
public interface Connection  extends Wrapper, AutoCloseable {
    Statement createStatement() throws SQLException;
    PreparedStatement prepareStatement(String sql) throws SQLException;
}

2个产品等级结构

  • java.sql.Statement;
  • java.sql.PreparedStatement;
public interface Statement extends Wrapper, AutoCloseable {...}
public interface PreparedStatement extends Statement {...}

具体工厂

  • com.mysql.jdbc.ConnectionImpl生产MySQL产品族的产品;
package com.mysql.jdbc;
public class ConnectionImpl extends ConnectionPropertiesImpl implements MySQLConnection {
    public java.sql.Statement createStatement() throws SQLException {
        return createStatement(DEFAULT_RESULT_SET_TYPE, DEFAULT_RESULT_SET_CONCURRENCY);
    }
    public java.sql.PreparedStatement prepareStatement(String sql) throws SQLException {
        return prepareStatement(sql, DEFAULT_RESULT_SET_TYPE, DEFAULT_RESULT_SET_CONCURRENCY);
    }
}

具体产品

  • com.mysql.jdbc.StatementImpl;
  • com.mysql.jdbc.ServerPreparedStatement;
package com.mysql.jdbc;
public class StatementImpl implements Statement {...}
public class ServerPreparedStatement extends PreparedStatement {...}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • JDBC基础知识 一、采用JDBC访问数据库的基本步骤: A.载入JDBC驱动程序 B.定义连接URL ...
    java日记阅读 3,902评论 0 20
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,583评论 0 4
  • 本文内容 1.什么是JDBC以及为什么要使用JDBC 2.JDBC核心API的讲解 3.使用JDBC核心API进行...
    Vincilovfang阅读 1,234评论 0 11
  • 懒青蛙阅读 118评论 0 0
  • 一、Window对象 浏览器对象模型 (BOM) 使 JavaScript 有能力与浏览器“对话”。 Window...
    追逐_e6cf阅读 270评论 0 9