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产品族;
抽象工厂
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 {...}