简单来说,什么是工厂? [英] In simplest terms, what is a factory?

查看:137
本文介绍了简单来说,什么是工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

你熟悉一个工厂,为什么要使用一个? JDBC:http://download.oracle.com/javase/tutorial/jdbc/basics/index.htmlrel =noreferrer>这是一个(全部)(抽象的)工厂。这是一个很好的现实世界的例子。

  //工厂方法。通过给定的类名加载驱动程序。它实际上返回一个
//具体的Class&Driver。但是,我们不需要它,所以我们忽略它。
//它可以是任何驱动程序类名。这里的MySQL只是一个例子。
//在封面下,它将执行DriverManager.registerDriver(新的Driver())。
Class.forName(com.mysql.jdbc.Driver);

//抽象工厂。这样,驱动程序可以为
//给定的URL返回具体的连接。您可以只针对java.sql.Connection接口声明它。
//在封面下,DriverManager将通过URL找到MySQL驱动程序,并调用
// driver.connect(),并返回新的ConnectionImpl()。
连接连接= DriverManager.getConnection(url);

//抽象工厂。这使得驱动程序从
//连接返回一个具体的语句。您可以只针对java.sql.Statement接口声明它。
//在封面下,MySQL ConnectionImpl将返回新的StatementImpl()。
语句语句= connection.createStatement();

//抽象工厂。这样可以让驱动程序从
//语句返回具体的结果集。您可以对java.sql.ResultSet接口声明它。
//在封面下,MySQL StatementImpl将返回新的ResultSetImpl()。
ResultSet resultSet = statement.executeQuery(sql);

您不需要单行JDBC驱动程序 import 在你的代码。您不需要执行 import com.mysql.jdbc.ConnectionImpl 或某事。您只需要根据 java.sql。* 声明所有内容。你不需要自己做 connection = new ConnectionImpl(); 您只需要从抽象工厂获取它作为标准API的一部分。



如果将JDBC驱动程序类名称设置为可以从外部配置的变量(例如属性文件)并编写ANSI兼容的SQL查询,那么您不需要重写,为世界上所知道的每个单一数据库供应商和/或JDBC驱动程序重新编译,重建和重新分配您的Java应用程序。您只需在运行时类路径中放置所需的JDBC驱动程序JAR文件,并通过某些(属性)文件提供配置,而无需更改任何一行Java代码,只要您要切换数据库或在不同的数据库中重新使用该应用程序。



这是界面和抽象工厂的力量。



另一个已知的真实世界示例是Java EE。将Java EE和JDBC驱动程序替换为Java EE应用程序服务器(WildFly,TomEE,GlassFish,Liberty等)。



另请参阅:




What is a factory and why would I want to use one?

解决方案

Are you familiar with JDBC? It's one and all (abstract) factory. It's a good real world example.

// Factory method. Loads the driver by given classname. It actually returns a 
// concrete Class<Driver>. However, we don't need it here, so we just ignore it.
// It can be any driver class name. The MySQL one here is just an example.
// Under the covers, it will do DriverManager.registerDriver(new Driver()).
Class.forName("com.mysql.jdbc.Driver");

// Abstract factory. This lets the driver return a concrete connection for the
// given URL. You can just declare it against java.sql.Connection interface.
// Under the covers, the DriverManager will find the MySQL driver by URL and call
// driver.connect() which in turn will return new ConnectionImpl().
Connection connection = DriverManager.getConnection(url);

// Abstract factory. This lets the driver return a concrete statement from the
// connection. You can just declare it against java.sql.Statement interface.
// Under the covers, the MySQL ConnectionImpl will return new StatementImpl().
Statement statement = connection.createStatement();

// Abstract factory. This lets the driver return a concrete result set from the
// statement. You can just declare it against java.sql.ResultSet interface.
// Under the covers, the MySQL StatementImpl will return new ResultSetImpl().
ResultSet resultSet = statement.executeQuery(sql);

You do not need to have a single line of JDBC driver specific import in your code. You do not need to do import com.mysql.jdbc.ConnectionImpl or something. You just have to declare everything against java.sql.*. You do not need to do connection = new ConnectionImpl(); yourself. You just have to get it from an abstract factory as part of a standard API.

If you make the JDBC driver class name a variable which can be configured externally (e.g. properties file) and write ANSI compatible SQL queries, then you do not ever need to rewrite, recompile, rebuild and redistribute your Java application for every single database vendor and/or JDBC driver which the world is aware of. You just have to drop the desired JDBC driver JAR file in the runtime classpath and provide configuration by some (properties) file without the need to change any line of Java code whenever you want to switch of DB or reuse the app on a different DB.

That's the power of interfaces and abstract factories.

Another known real world example is Java EE. Substitute "JDBC" with "Java EE" and "JDBC driver" with "Java EE application server" (WildFly, TomEE, GlassFish, Liberty, etc).

See also:

这篇关于简单来说,什么是工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆