實現DAO 設計模式
?
在JAVA編程的時候, 有時候看起來非常直接的實現卻非要用設計模式轉若干個彎去實現他, 這似乎顯的很多余,但是采用一些成熟的設計模式,會使程序更加的健壯,松耦合以及好維護和擴展.
實現DAO 設計模式 為DAO實現工廠類的策略 1 采用工廠方法設計模式 如果一個DAO 工廠只為一個數據庫的實現,(比如ORACLE)而創建很多的DAO的時候,實現該策略時,我們考慮采用工廠方法設計模式. 假設該工廠類創建了CustomerDAO, AccountDAO, OrderDAO 等一些對象。 2 使用抽象工廠設計模式: 如果考慮為三種不同類型的數據庫來實現這個策略,我們可以考慮采用抽象工廠設計模式. 假設. 這個工廠創建了CustomerDAO, AccountDAO, OrderDAO的一系列的DAO, 該策略運用了在抽象工廠中產生的工廠類中的工廠方法的實現. 代碼說明: 以下代碼舉例說明了DAO設計模式的具體實現: 我們以使用抽象工廠的設計模式來對付多種類型數據庫為例,在以下的例子中只具體列出CLOUDSCAPE 數據庫類型的DAO設計模式的具體實現,其他類型數據庫DAO設計模式的實現大同小異. 1 // 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. // 所有實現該抽象工廠的工廠類中必須有的方法,用這些方法來創建具體的DAO類. public abstract CustomerDAO getCustomerDAO(); public abstract AccountDAO getAccountDAO(); public abstract OrderDAO getOrderDAO(); //該抽象類的靜態方法,用他來創建其他具體的DAO工廠類 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; } } } 2 以下是Cloudscape DAO FACTORY 類的實現,在他里面實現了該類型數據庫的連接,以及實現了他所繼承的抽象工廠類中所必須實現的那些方法,在這些方法中創建具體的DAO對象. // 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 //建立Cloudscape 連接 public static Connection createConnection() { // Use DRIVER and DBURL to create a connection // Recommend connection pool implementation/usage } //創建 CustomerDAO 對象 當然返回的是一個該類實現的接口,他的好處就是實現了實現細節的隱蔽. public CustomerDAO getCustomerDAO() { // CloudscapeCustomerDAO implements CustomerDAO return new CloudscapeCustomerDAO(); } //創建 AccountDAO 對象 當然返回的是一個該類實現的接口,他的好處就是實現了實現細節的隱蔽. public AccountDAO getAccountDAO() { // CloudscapeAccountDAO implements AccountDAO return new CloudscapeAccountDAO(); } //創建 OrderDAO 對象 當然返回的是一個該類實現的接口,他的好處就是實現了實現細節的隱蔽. public OrderDAO getOrderDAO() { // CloudscapeOrderDAO implements OrderDAO return new CloudscapeOrderDAO(); } ... } 3 以下代碼就是具體DAO類實現的接口也就是CloudscapeCustomerDAO()實現的接口: CustomerDAO .在該接口中定義了所有的業務方法. // 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 selectCustomersTO(...); ... } 4 以下CloudscapeCustomerDAO類實現的具體業務細節和數據操作細節, 他是要向客戶數據端隱蔽的. 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 Transfer Object if found, // return null on error or if not found } public boolean updateCustomer(...) { // implement update record here using data // from the customerData Transfer 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 selectCustomersTO(...) { // implement search customers here using the // supplied criteria. // Alternatively, implement to return a Collection // of Transfer Objects. } ... } 5 下面的代碼是數據客戶端向DAO中傳輸數據的, 他其實就是一個JAVABEAN; public class Customer implements java.io.Serializable { // member variables int CustomerNumber; String name; String streetAddress; String city; ... // getter and setter methods... ... } 6 最后就是客戶數據端對這個設計的應用: ... // 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 Transfer Object. Customer cust = custDAO.findCustomer(...); // modify the values in the Transfer 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("New York"); Collection customersList = custDAO.selectCustomersTO(criteria); // returns customersList - collection of Customer // Transfer Objects. iterate through this collection to // get values. 言而簡之,以下6步完成該模式的實現: 1 創建一個抽象工廠類,他包含兩個重要的部分: 第一部分是 一些抽象方法,這些方法是所有實現該抽象工廠的具體工廠類所必須實現的. 第二部分 就是一個靜態方法,該方法來創建一個具體類型數據源的工廠對象,比如文中的CloudscapeDAOFactory(). 2 然后,分別創建各個類型數據源的工廠類,(本文以CloudscapeDAOFactory為例).在這個工廠類中里面也有兩個重要組成部分: 第一部分就是實現在他繼承的那個抽象工廠類中的左右抽象方法,在該方法中創建具體的DAO對象(這些對象的類在第4不具體定義實現),本文中三個方法分別創建了3個具體的DAO對象,當然為了實現細節的隱蔽,這些方法返回的是這些具體DAO類門實現的接口(這些接口在第3步實現). 3 定義具體DAO類的接口,并在接口中定義所有的業務方法,和數據操作方法. 4 定義具體的DAO類,在這個類中才是實際的業務方法,和數據的操作的實現. 5 定義數據傳輸對象,他是用來在客戶端和DAO之間傳遞數據的,他其實就是一個JAVABEAN. 6 完成以上5步之后我們就可以在數據客戶端使用以上由DAO設計模式定義好的各個類了(見最后一個代碼例子塊). 以上6步大家在編程的時需具體體會,一般來說,數據庫中的一個表就可以對應一個數據傳遞類也就是在第4步中定義的那個類,類中的屬性就是表中的字段,然后加上相應的GET,SET 方法. 然后再按模式和以上步驟來定義具體的類. 本文譯自:http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html jegg JAVA-J2EE-DESIGH PATTERN 共享就是力量! |