为DAO实现工厂类的策略
1 采用工厂方法设计模式
如果一个DAO 工厂只为一个数据库的实现,(比如ORACLE)而创建很多的DAO的时候,实现该策略时,我们考虑采用工厂方法设计模式. 假设该工厂类创建了CustomerDAO,AccountDAO, OrderDAO 等一些对象。
2 使用抽象工厂设计模式:
如果考虑为三种不同类型的数据库来实现这个策略,我们可以考虑采用抽象工厂设计模式. 假设. 这个工厂创建了CustomerDAO,AccountDAO, OrderDAO的一系列的DAO, 该策略运用了在抽象工厂中产生的工厂类中的工厂方法的实现.
代码说明:
以下代码举例说明了DAO设计模式的具体实现:
我们以使用抽象工厂的设计模式来对付多种类型数据库为例,在以下的例子中只具体列出CLOUDSCAPE 数据库类型的DAO设计模式的具体实现,其他类型数据库DAO设计模式的实现大同小异.
1. // Abstract class DAO Factory
2. public abstract class DAOFactory {
3.
4. // List of DAO types supported by the factory
5. public static final int CLOUDSCAPE = 1;
6. public static final int ORACLE = 2;
7. public static final int SYBASE = 3;
8. ...
9.
10. // There will be a method for each DAO that can be
11. // created. The concrete factories will have to
12. // implement these methods.
13. // 所有实现该抽象工厂的工厂类中必须有的方法,用这些方法来创建具体的DAO类.
14. public abstract CustomerDAO getCustomerDAO();
15. public abstract AccountDAO getAccountDAO();
16. public abstract OrderDAO getOrderDAO();
17.
18. //该抽象类的静态方法,用他来创建其他具体的DAO工厂类
19. public static DAOFactory getDAOFactory(
20. int whichFactory) {
21.
22. switch (whichFactory) {
23. case CLOUDSCAPE:
24. return new CloudscapeDAOFactory();
25. case ORACLE :
26. return new OracleDAOFactory();
27. case SYBASE :
28. return new SybaseDAOFactory();
29. ...
30. default :
31. return null;
32. }
33. }
34. }
2 以下是Cloudscape DAO FACTORY类的实现,在他里面实现了该类型数据库的连接,以及实现了他所继承的抽象工厂类中所必须实现的那些方法,在这些方法中创建具体的DAO对象.
1. // Cloudscape concrete DAO Factory implementation
2. import java.sql.*;
3.
4. public class CloudscapeDAOFactory extends DAOFactory {
5. public static final String DRIVER=
6. "COM.cloudscape.core.RmiJdbcDriver";
7. public static final String DBURL=
8. "jdbc:cloudscape:rmi://localhost:1099/CoreJ2EEDB";
9.
10. // method to create Cloudscape connections
11. //建立Cloudscape 连接
12. public static Connection createConnection() {
13. // Use DRIVER and DBURL to create a connection
14. // Recommend connection pool implementation/usage
15. }
16. //创建 CustomerDAO 对象 当然返回的是一个该类实现的接口,他的好处就是实现了实现细节的隐蔽.
17. public CustomerDAO getCustomerDAO() {
18. // CloudscapeCustomerDAO implements CustomerDAO
19. return new CloudscapeCustomerDAO();
20. }
21. //创建 AccountDAO 对象 当然返回的是一个该类实现的接口,他的好处就是实现了实现细节的隐蔽.
22. public AccountDAO getAccountDAO() {
23. // CloudscapeAccountDAO implements AccountDAO
24. return new CloudscapeAccountDAO();
25. }
26. //创建 OrderDAO 对象 当然返回的是一个该类实现的接口,他的好处就是实现了实现细节的隐蔽.
27.
28. public OrderDAO getOrderDAO() {
29. // CloudscapeOrderDAO implements OrderDAO
30. return new CloudscapeOrderDAO();
31. }
32. ...
33. }
3 以下代码就是具体DAO类实现的接口也就是CloudscapeCustomerDAO()实现的接口: CustomerDAO .在该接口中定义了所有的业务方法.
1. // Interface that all CustomerDAOs must support
2. public interface CustomerDAO {
3. public int insertCustomer(...);
4. public boolean deleteCustomer(...);
5. public Customer findCustomer(...);
6. public boolean updateCustomer(...);
7. public RowSet selectCustomersRS(...);
8. public Collection selectCustomersTO(...);
9. ...
10. }
4 以下CloudscapeCustomerDAO类实现的具体业务细节和数据操作细节, 他是要向客户数据端隐蔽的.
1. import java.sql.*;
2. public class CloudscapeCustomerDAO implements
3. CustomerDAO {
4. public CloudscapeCustomerDAO() {
5. // initialization
6. }
7. // The following methods can use
8. // CloudscapeDAOFactory.createConnection()
9. // to get a connection as required
10. public int insertCustomer(...) {
11. // Implement insert customer here.
12. // Return newly created customer number
13. // or a -1 on error
14. }
15. public boolean deleteCustomer(...) {
16. // Implement delete customer here
17. // Return true on success, false on failure
18. }
19. public Customer findCustomer(...) {
20. // Implement find a customer here using supplied
21. // argument values as search criteria
22. // Return a Transfer Object if found,
23. // return null on error or if not found
24. }
25. public boolean updateCustomer(...) {
26. // implement update record here using data
27. // from the customerData Transfer Object
28. // Return true on success, false on failure or
29. // error
30. }
31. public RowSet selectCustomersRS(...) {
32. // implement search customers here using the
33. // supplied criteria.
34. // Return a RowSet.
35. }
36. public Collection selectCustomersTO(...) {
37. // implement search customers here using the
38. // supplied criteria.
39. // Alternatively, implement to return a Collection
40. // of Transfer Objects.
41. }
42. ...
43. }
5 下面的代码是数据客户端向DAO中传输数据的, 他其实就是一个JAVABEAN;
1. public class Customer implements java.io.Serializable {
2. // member variables
3. int CustomerNumber;
4. String name;
5. String streetAddress;
6. String city;
7. ...
8.
9. // getter and setter methods...
10. ...
11. }
6最后就是客户数据端对这个设计的应用:
1. ...
2. // create the required DAO Factory
3. DAOFactory cloudscapeFactory =
4. DAOFactory.getDAOFactory(DAOFactory.DAOCLOUDSCAPE);
5. // Create a DAO
6. CustomerDAO custDAO =
7. cloudscapeFactory.getCustomerDAO();
8. // create a new customer
9. int newCustNo = custDAO.insertCustomer(...);
10. // Find a customer object. Get the Transfer Object.
11. Customer cust = custDAO.findCustomer(...);
12. // modify the values in the Transfer Object.
13. cust.setAddress(...);
14. cust.setEmail(...);
15. // update the customer object using the DAO
16. custDAO.updateCustomer(cust);
17. // delete a customer object
18. custDAO.deleteCustomer(...);
19. // select all customers in the same city
20. Customer criteria=new Customer();
21. criteria.setCity("New York");
22. Collection customersList =
23. custDAO.selectCustomersTO(criteria);
24. // returns customersList - collection of Customer
25. // Transfer Objects. iterate through this collection to
26. // get values.
1 创建一个抽象工厂类,他包含两个重要的部分: 第一部分是一些抽象方法,这些方法是所有实现该抽象工厂的具体工厂类所必须实现的. 第二部分就是一个静态方法,该方法来创建一个具体类型数据源的工厂对象,比如文中的CloudscapeDAOFactory().
2 然后,分别创建各个类型数据源的工厂类,(本文以CloudscapeDAOFactory为例).在这个工厂类中里面也有两个重要组成部分: 第一部分就是实现在他继承的那个抽象工厂类中的左右抽象方法,在该方法中创建具体的DAO对象(这些对象的类在第4不具体定义实现),本文中三个方法分别创建了3个具体的DAO对象,当然为了实现细节的隐蔽,这些方法返回的是这些具体DAO类门实现的接口(这些接口在第3步实现).
3 定义具体DAO类的接口,并在接口中定义所有的业务方法,和数据操作方法.
4 定义具体的DAO类,在这个类中才是实际的业务方法,和数据的操作的实现.
5 定义数据传输对象,他是用来在客户端和DAO之间传递数据的,他其实就是一个JAVABEAN.
6 完成以上5步之后我们就可以在数据客户端使用以上由DAO设计模式定义好的各个类了(见最后一个代码例子块).
以上6步大家在编程的时需具体体会,一般来说,数据库中的一个表就可以对应一个数据传递类也就是在第4步中定义的那个类,类中的属性就是表中的字段,然后加上相应的GET,SET 方法. 然后再按模式和以上步骤来定义具体的类.