设计模式系列之工厂模式

设计模式系列之工厂模式简单工厂模式下面看下JDBC中获取Connection的代码publicclassConnectionFactory{publicConnect

大家好,欢迎来到IT知识分享网。

工厂模式包括了简单工厂、工厂方法和抽象工厂。下面我从java实际应用的角度分别介绍这三种模式。

简单工厂模式

下面看下JDBC中获取Connection的代码

public class ConnectionFactory { public Connection createConnection(String dbType,String serverName,String dbName,String userName,String password) throws SQLException { if(dbType.equalsIgnoreCase("mysql")) { try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } String url = "jdbc:mysql://"+serverName+":3306/"+dbName +"?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT"; return DriverManager.getConnection(url,userName,password); } else if(dbType.equalsIgnoreCase("postgresql")) { try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } String url = "jbdc:postgresql://"+serverName+":5432/"+dbName; return DriverManager.getConnection(url,userName,password); } else if(dbType.equalsIgnoreCase("MariaDB")) { try { Class.forName("org.mariadb.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } String url = "jdbc:mariadb://"+serverName+":3306/"+dbName; return DriverManager.getConnection(url,userName,password); } else { throw new IllegalArgumentException("未知的dbType参数类型"); } } } 

缺点:违背设计原则:对扩展开放,对修改关闭。因为假如我业务需要新增一个数据库Connection 获取方式就得修改这部分的代码。

工厂方法模式

下面我们针对普通工厂模式的缺点进行优化。

我们可以定义一个工厂方法接口IConnectionFactory ,包含一个方法,交给子类去实现各自的Connection创建方法

public interface IConnectionFactory { Connection create(String serverName,String dbName,String userName,String password) throws SQLException; } 创建PostgreSqlConnectionFactory工厂并实现IConnectionFactory接口 public class PostgreSqlConnectionFactory implements IConnectionFactory { @Override public Connection create(String serverName, String dbName, String userName, String password) throws SQLException { try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } String url = "jbdc:postgresql://"+serverName+":5432/"+dbName; return DriverManager.getConnection(url,userName,password); } } 

创建MySqlConnectionFactory 工厂并实现IConnectionFactory接口

public class MySqlConnectionFactory implements IConnectionFactory { @Override public Connection create(String serverName, String dbName, String userName, String password) throws SQLException { try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } String url = "jdbc:mysql://" + serverName + ":3306/" + dbName + "?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT"; return DriverManager.getConnection(url, userName, password); } } 

创建MariaDBConnectionFactory 工厂并实现IConnectionFactory接口

public class MariaDBConnectionFactory implements IConnectionFactory { @Override public Connection create(String serverName, String dbName, String userName, String password) throws SQLException { try { Class.forName("org.mariadb.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } String url = "jdbc:mariadb://"+serverName+":3306/"+dbName; return DriverManager.getConnection(url,userName,password); } } 

测试方法

Connection conn = new MySqlConnectionFactory().create("127.0.0.1", "test", "root", "root"); 

工厂方法模式的优点:新增一种类型,只需增加一个工厂,并实现抽象工厂即可。

缺点就是调用者需要知道调用的子类对象对应的子类工厂。

抽象工厂

上述的一个工厂对应一个产品,如果一个工厂对应多个产品那就是我们的抽象工厂模式了。比如 Connection 接口就是应用了抽象工厂模式。其中的方法都是工厂方法,比如:createStatement、prepareStatement、prepareCall等他们都有对应的实现类。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/56955.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信