很多的J2EE應用程序需要使用持久性數(shù)據(jù)(數(shù)據(jù)庫、文件等)。不同的程序,持久性存儲是各不相同的,并且用來訪問這些不同的持久性存儲機制的API也有很大的不同。如果應用程序要在不同的持久性存儲間遷移,這些訪問特定持久存儲層的代碼將面臨重寫。
如何解決這個問題?且看"DAO模式"
數(shù)據(jù)訪問對象(Data Acess Object) 模式
一.環(huán)境
根據(jù)數(shù)據(jù)源不同,數(shù)據(jù)訪問也不同。根據(jù)存儲的類型(關系數(shù)據(jù)庫、面向?qū)ο髷?shù)據(jù)庫、文件等等)和供應商實現(xiàn)不同,持久性存儲(比如數(shù)據(jù)庫)的訪問差別也很大。
二.問題
許多真是的J2EE應用程序需要在一定程度上使用持久性數(shù)據(jù)。對于許多應用程序,持久性存儲是使用不同的機制實現(xiàn)的,并且用來訪問這些不同的持久性存儲機制的API也有很大的不同。
比如,應用程序使用實體bean(這里應該是指BMP的bean,CMP的bean已大大降低了與RDBMS的耦合)的分布式組件來表示持久性數(shù)據(jù),或者使用JDBC API來訪問駐留在某關系數(shù)據(jù)庫管理系統(tǒng)(RDBMS)中的數(shù)據(jù),這些組件中包含連接性性和數(shù)據(jù)訪問代碼會引入這些組件與數(shù)據(jù)源實現(xiàn)之間的緊密耦合。組件中這類代碼依賴性使應用程序從某種數(shù)據(jù)源遷移到其他種類的數(shù)據(jù)源將變得非常麻煩和困難。當數(shù)據(jù)源變化時,組件也需要改變,以便于能夠處理新類型的數(shù)據(jù)源。
(舉個例子來說,我們UPTEL系統(tǒng)是使用JDBC API對 ORACLE數(shù)據(jù)庫進行連接和數(shù)據(jù)訪問的,這些JDBC API與SQL語句散布在系統(tǒng)中,當我們需要將UPTEL遷移到其他RDBMS時,比如曾經(jīng)遷移到INFORMIX,就面臨重寫數(shù)據(jù)庫連接和訪問數(shù)據(jù)的模塊。)
三.作用力
1.諸如bean管理的實體bean、會話bean、servlet等組件往往需要從持久性存儲數(shù)據(jù)源中檢索數(shù)據(jù),以及進行數(shù)據(jù)存儲等操作。
2.根據(jù)產(chǎn)品供應商的不同,持久性存儲API差別也很大,這些API和其能力同樣根據(jù)存儲的類型不同也有差別,這樣存在以下缺點,即訪問這些獨立系統(tǒng)的API很不統(tǒng)一。
3.組件需要透明于實際的持久性存儲或者數(shù)據(jù)源實現(xiàn),以便于提供到不同供應商產(chǎn)品、不同存儲類型和不同數(shù)據(jù)源類型的更容易的移植性。
四.解決方案
使用數(shù)據(jù)訪問對象(DAO)模式來抽象和封裝所有對數(shù)據(jù)源的訪問。DAO管理著與數(shù)據(jù)源的連接以便檢索和存儲數(shù)據(jù)。
DAO實現(xiàn)了用來操作數(shù)據(jù)源的訪問機制。數(shù)據(jù)源可以時RDBMS,LDAP,File等。依賴于DAO的業(yè)務組件為其客戶端使用DAO提供更簡單的接口。DAO完全向客戶端隱藏了數(shù)據(jù)源實現(xiàn)細節(jié)。由于當?shù)蛯訑?shù)據(jù)源實現(xiàn)變化時,DAO向客戶端提供的接口不會變化,所有該模式允許DAO調(diào)整到不同的存儲模式,而不會影響其客戶端或者業(yè)務組件。重要的是,DAO充當組件和數(shù)據(jù)源之間的適配器。
(按照這個理論,如果我們UPTEL系統(tǒng)使用了DAO模式,就可以無縫的從ORACLE遷移到任何一個RDBMS了。夢想總是很完美的,且看看DAO模式如何實現(xiàn))
1.結(jié)構,圖1是表示DAO模式中各種關系的類圖。
此主題相關圖片如下:
2.參與者和職責
1)BusinessObject(業(yè)務對象)
代表數(shù)據(jù)客戶端。正是該對象需要訪問數(shù)據(jù)源以獲取和存儲數(shù)據(jù)。
2)DataAccessObject(數(shù)據(jù)訪問對象)
是該模式的主要對象。DataAccessObject抽取該BusinessObject的低層數(shù)據(jù)訪問實現(xiàn),以保證對數(shù)據(jù)源的透明訪問。BusinessObject也可以把數(shù)據(jù)加載和存儲操作委托給DataAccessObject。
3)DataSource(數(shù)據(jù)源)
代表數(shù)據(jù)源實現(xiàn)。數(shù)據(jù)源可以是各RDBMSR數(shù)據(jù)庫,OODBMS,XML文件等等。
4)valueObject(值對象)
代表用做數(shù)據(jù)攜帶著的值對象。DataAccessObject可以使用值對象來把數(shù)據(jù)返回給客戶端。
DataAccessObject也許會接受來自于客戶端的數(shù)據(jù),其中這些用于更新數(shù)據(jù)源的數(shù)據(jù)存放于值對象中來傳遞。
3.策略
1).自動DAO代碼產(chǎn)生策略
因為每個BusinessObject對應于一個特殊的DAO,因此有可能建立BusinessObject,DAO和低層實現(xiàn)(比如RDBMS中的表)之間的關系(映射)。一點這些關系(映射)已經(jīng)建立,我們就可以編寫與應用程序有館的代碼生成的簡單工具了(什么?自己寫GP程序?用ORM的附帶工具自動生成不就完了,最多自己寫幾個Adapter,牛人就是不同,啥都要自己寫...),其中的工具可以產(chǎn)生該應用程序需要的所有DAO代碼。
如果DAO需求很復雜,我們可以采用第三方工具,其中這些工具提供對象到RDBMS數(shù)據(jù)庫的關系映射(這里指的是前面提到的ORM工具,全稱是Object Relation Mapping,目前成熟的ORM工具有很多:Hibernate,OJB,Torque,TopLink等等)。
這些工具通常包含GUI工具來把業(yè)務對象映射到持久性存儲對象,并且因而定義中間DAO。一旦這些映射完成,這些工具會自動地生成代碼,并且也許會提供其他增值功能,比如結(jié)果緩沖、查詢緩沖、與應用程序集成,以及與其他第三方產(chǎn)品(比如分布式緩沖)地繼承,等等。
(增值服務:Torque提供了結(jié)果緩沖,Hibernate提供了對Oracle數(shù)據(jù)庫SQL指令的優(yōu)化,OJB提供JDO API、OMDB API)
2).數(shù)據(jù)訪問對象的工廠策略
通過調(diào)整抽象工廠和工廠方法模式,DAO模式可以達到很高的靈活度。
當?shù)蛯哟鎯Σ粫S著實現(xiàn)變化而變化時,該策略可以使用工廠方法模式來實現(xiàn)該策略。以產(chǎn)生應用程序需要的大量DAO。圖2是這種情況下的類圖。
此主題相關圖片如下:
當?shù)蛯哟鎯﹄S著實現(xiàn)變化而變化時,該策略可以使用抽象工廠方法模式而實現(xiàn)。
圖3是這種情況下的類圖。
此主題相關圖片如下:
5.結(jié)果
1).啟用透明性
業(yè)務對象可以是使用數(shù)據(jù)源,而無須了解該數(shù)據(jù)源實現(xiàn)的具體細節(jié)。訪問是透明的,原因是實現(xiàn)被隱藏在DAO的內(nèi)部。
2).啟用更容易的遷移
DAO層使應用程序更加容易地遷移到一個不同的數(shù)據(jù)庫實現(xiàn)。業(yè)務對象不了解低層數(shù)據(jù)實現(xiàn)。因而,該遷移只涉及對DAO層的變化。更進一步說,如果使用工廠策略,則有可能為每一個低層存儲實現(xiàn)提供一個具體工廠實現(xiàn)。在這種情況下,遷移到不同的遷移實現(xiàn)意味著給應用程序提供一個新的工廠實現(xiàn)。
3).減少業(yè)務對象中代碼復雜度
由于DAO管理所有的數(shù)據(jù)訪問復雜性,它可以簡化業(yè)務對象和其他使用DAO的客戶端中的代碼。所有與實現(xiàn)有關的代碼(比如sql語句)都被包含在DAO中,而不是包含在業(yè)務對象中。這樣做提高了代碼的可讀性,已經(jīng)代碼生產(chǎn)效率。
4).把所有的數(shù)據(jù)訪問集中到一個獨立的層。
因為所有的數(shù)據(jù)訪問操作現(xiàn)在被委托給DAO,所有單獨的數(shù)據(jù)訪問層可以被看作把數(shù)據(jù)訪問實現(xiàn)與應用程序中的其他代碼相隔離的。這種集中化使應用程序更容易地維護和管理。
5).不適用于容器管理的持久性
由于EJB容器用容器管理的持久性(CMP)來管理實體bean,該容器會自動地服務所有的持久性存儲訪問。使用容器管理的實體bean的應用程序不需要DAO層,因為該應用程序服務器透明地提供該功能。然而,當需要組合使用CMP和BMP時,DAO仍舊有用處。
6).添加其他層
DAO會在數(shù)據(jù)客戶端和數(shù)據(jù)源之間創(chuàng)建其他的對象層,其中該數(shù)據(jù)源需要被設計和實現(xiàn)以便于權衡該模式的好處。但是選擇本方法也會帶來額外的開銷。
7).需要類層次設計
在使用工廠策略時,我們需要設計和實現(xiàn)具體工廠的層次,以及這些工廠產(chǎn)生的具體產(chǎn)品層次。如果能夠確保這種靈活性,則有必要考慮這種額外的工作。這樣做會增加設計的復雜性。然而,在實現(xiàn)該工廠策略時,你可以首先考慮工廠方法模式,然后再根據(jù)需要過渡到抽象工廠。
六.范例代碼
1.實現(xiàn)數(shù)據(jù)訪問對象模式
范例9-4時表示Customer信息的持久性對象的DAO范例代碼。當findCustomer()被調(diào)用時,CloudscapeCustomerDAO創(chuàng)建一個Customer值對象。
范例9-6是使用DAO的范例代碼。
2.實現(xiàn)數(shù)據(jù)訪問對象的工廠策略
1)使用工廠方法模式
2)使用抽象工廠模式
范例代碼9-2是CloudscapeDAOFactory的范例代碼。
范例代碼9-3中的CustomerDAO接口為Customer持久性對象定義了DAO方法,這些接口是被所有具體DAO實現(xiàn)來實現(xiàn)的,比如CloudscapeCustomerDAO、OracleCustomerDAO、已經(jīng)SybaseCustomerDAO。Account和OrederDAO接口也與此類似。
Example 9.1 Abstract DAOFactory Class
// Abstract class DAO Factory
public abstract class DAOFactory {
// List of DAO types supported by the factory
public static final int CLOUDSCAPE = 1;
public static final int ORACLE = 2;
public static final int SYBASE = 3;
...
// There will be a method for each DAO that can be
// created. The concrete factories will have to
// implement these methods.
public abstract CustomerDAO getCustomerDAO();
public abstract AccountDAO getAccountDAO();
public abstract OrderDAO getOrderDAO();
...
public static DAOFactory getDAOFactory(
int whichFactory) {
switch (whichFactory) {
case CLOUDSCAPE:
return new CloudscapeDAOFactory();
case ORACLE :
return new OracleDAOFactory();
case SYBASE :
return new SybaseDAOFactory();
...
default :
return null;
}
}
}
Example 9.2 Concrete DAOFactory Implementation for Cloudscape
// Cloudscape concrete DAO Factory implementation
import java.sql.*;
public class CloudscapeDAOFactory extends DAOFactory {
public static final String DRIVER=
"COM.cloudscape.core.RmiJdbcDriver";
public static final String DBURL=
"jdbc:cloudscape:rmi://localhost:1099/CoreJ2EEDB";
// method to create Cloudscape connections
public static Connection createConnection() {
// Use DRIVER and DBURL to create a connection
// Recommend connection pool implementation/usage
}
public CustomerDAO getCustomerDAO() {
// CloudscapeCustomerDAO implements CustomerDAO
return new CloudscapeCustomerDAO();
}
public AccountDAO getAccountDAO() {
// CloudscapeAccountDAO implements AccountDAO
return new CloudscapeAccountDAO();
}
public OrderDAO getOrderDAO() {
// CloudscapeOrderDAO implements OrderDAO
return new CloudscapeOrderDAO();
}
...
}
Example 9.3 Base DAO Interface for Customer
// Interface that all CustomerDAOs must support
public interface CustomerDAO {
public int insertCustomer(...);
public boolean deleteCustomer(...);
public Customer findCustomer(...);
public boolean updateCustomer(...);
public RowSet selectCustomersRS(...);
public Collection selectCustomersVO(...);
...
}
Example 9.4 Cloudscape DAO Implementation for Customer
// CloudscapeCustomerDAO implementation of the
// CustomerDAO interface. This class can contain all
// Cloudscape specific code and SQL statements.
// The client is thus shielded from knowing
// these implementation details.
import java.sql.*;
public class CloudscapeCustomerDAO implements
CustomerDAO {
public CloudscapeCustomerDAO() {
// initialization
}
// The following methods can use
// CloudscapeDAOFactory.createConnection()
// to get a connection as required
public int insertCustomer(...) {
// Implement insert customer here.
// Return newly created customer number
// or a -1 on error
}
public boolean deleteCustomer(...) {
// Implement delete customer here
// Return true on success, false on failure
}
public Customer findCustomer(...) {
// Implement find a customer here using supplied
// argument values as search criteria
// Return a value object if found,
// return null on error or if not found
}
public boolean updateCustomer(...) {
// implement update record here using data
// from the customerData value object
// Return true on success, false on failure or
// error
}
public RowSet selectCustomersRS(...) {
// implement search customers here using the
// supplied criteria.
// Return a RowSet.
}
public Collection selectCustomersVO(...) {
// implement search customers here using the
// supplied criteria.
// Alternatively, implement to return a Collection
// of value objects.
}
...
}
Example 9.5 Customer value Object
public class Customer implements java.io.Serializable {
// member variables
int CustomerNumber;
String name;
String streetAddress;
String city;
...
// getter and setter methods...
...
}
Example 9.6 Using a DAO and DAO Factory ?Client Code
...
// create the required DAO Factory
DAOFactory cloudscapeFactory =
DAOFactory.getDAOFactory(DAOFactory.DAOCLOUDSCAPE);
// Create a DAO
CustomerDAO custDAO =
cloudscapeFactory.getCustomerDAO();
// create a new customer
int newCustNo = custDAO.insertCustomer(...);
// Find a customer object. Get the value object.
Customer cust = custDAO.findCustomer(...);
// modify the values in the value object.
cust.setAddress(...);
cust.setEmail(...);
// update the customer object using the DAO
custDAO.updateCustomer(cust);
// delete a customer object
custDAO.deleteCustomer(...);
// select all customers in the same city
Customer criteria=new Customer();
criteria.setCity("廣州");
Collection customersList =
custDAO.selectCustomersVO(criteria);
// returns customersList - collection of Customer
// value objects. iterate through this collection to
// get values.
...
七.相關模式
1.值對象
2.工廠方法
3.代理