Recently participated in the web programming project, use the Struts framework, decide to use the DAO design mode when processed to data persistence, so the DAO design mode in the J2EE core design mode of Sun is read, and the translation part is for everyone to share, not enough to indicate I hope everyone should point out that I will help you.
When Java programming, sometimes it looks very direct, but it is necessary to use the design model to turn a few bends to achieve him, which seems to have a lot of more, but use some mature design patterns, which will make the program more robust. Loose coupling and good maintenance and expansion.
Implement DAO design mode
Factory class strategy for DAO
1 use factory method design mode
If a DAO factory creates a lot of DAOs for a DAO factory, (such as Oracle), we consider the use of factory method design mode. Suppose the plant class creates some objects such as CustomerDao, Accountdao, OrderDao. .
2 use abstract factory design patterns:
If you consider this strategy for three different types of databases, we can consider using abstract factory design patterns. Suppose. This factory created a series of DAOs for CustomerDao, Accountdao, ORDERDAO, which used the strategy to generate it in abstract factories. Implementation of Factory Methods in Factory Class.
Code description:
The following code illustrates the specific implementation of the DAO design mode:
We use the abstract factory design model to deal with multiple types of databases as an example, and only the specific implementation of the DAO design pattern of the Cloudscape database type is specifically realized in the following examples, and other types of database DAO design patterns are similar.
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 contrete factory for the concrete factories Will Have To TO
// Implement these Methods.
// All methods must be achieved in the factory class that implements the abstract factory, with these methods to create specific DAO classes.
Public Abstract Customerdao getcustomerdao ();
Public Abstract Accountdao getaccountdao ();
Public Abstract OrderDao getorderdao ();
// The abstract class static method uses him to create other specific DAO factory classes
Public Static Daofactory GetDaOfactory
INT whichfactory) {
Switch (WhichFactory) {
Case Cloudscape:
Return New Cloudscape CapedaFactory ();
Case Oracle:
Return new oracledaofactory ();
Case Sybase:
Return New SybasedaOfactory ();
...
DEFAULT:
Return NULL;
}
}
}
2 The following is the implementation of the Cloudscape DAO Factory class, which implements the connection of this type of database, and the methods that must be implemented in the abstract factory class inherited, and create specific DAO objects in these methods.
// Cloudscape Concrete Dao Factory Implementation
Import java.sql. *;
Public class cloudscapecapedaofactory 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
// Establish a Cloudscape connection
Public static connection createConnection () {
// use driver and dburl to create a connection
// Recommend Connection Pool Implementation / Usage
}
// Creating a CustomerDao object Returns an interface that is implemented, and his benefit is to achieve the coverage of the implementation.
Public Customerdao getcustomerdao () {
// CloudscapeCaScaScaScustomerdao Implements Customerdao
Return New CloudscapeCustomerdao ();
}
// Creating an Accountdao object Of course, it is an interface that is implemented. His advantage is to achieve the coverage of the details.
Public Accountdao getaccountdao () {
// CloudscapeCapeaccountdao Implements Accountdao
Return New CloudscapeAccountdao ();
}
// Creating an ORDERDAO object Of course, it is an interface that is implemented. His advantage is to achieve the coverage of details.
Public orderdao getorderdao () {
// CloudscapeRDERDAO IMPLEMENTS Orderdao
Return New CloudscapeOrdao ();
}
...
}
3 The following code is that the interface implemented by the specific DAO class is also the interface implemented by CloudscapeCustomerDao (): CustomerDao. All business methods are defined in this interface.
// 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 The specific business details and data operation details of the following CloudscapeCaPecustomerDao classes, he is hidden to the customer data.
Import java.sql. *;
Public Class CloudscapeCustomerDao Implements
Customerdao {
Public CloudscapeCaPecuScustomerdao () {
// Initialization
}
// the folowing methods can use
// CloudscapeCaPedaOfactory.createConnection ()
// TO GET A connection as request
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 (...) {
//Mplement 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.
// Alternative, Implement to Return A Collection
// of transfer objects.
}
...
}
5 The following code is that the data client transmits data to DAO, he is actually a javabean;
Public class customer imports java.io.serializable {
// Member Variables
INT Customernumber;
String name;
String streetaddress;
String city;
...
// getter and setter methods ...
...
}
6 The last is the application of the customer data to this design:
...
// 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 sale the dau
Custdao.UpdateCustomer (Cust);
// delete a ready Object
Custdao.Deletecustomer (...);
// Select All Customers in The Same Citycustomer criteria = New Customer ();
Criteria.SetCity ("New York");
Collection CustomersList =
Custdao.selectcustomersto (criteria);
// Returns Customerslist - Collection of Customer
// Transfer Objects. Iteerate Through this Collection TO
// Get Values.
Simply, the following 6 steps complete the implementation of this mode:
1 Create an abstract factory class, he contains two important parts: The first part is some abstract method, which is all necessary to achieve the specific factory class of the abstract factory. The second part is a static method, the method comes Create a factory object for a specific type of data source, such as CloudscapeCaPedaOfactory () in the article.
2 Then, the factory class of each type of data source is created, and it is also an example of CloudscapeDaOfactory. There are two important components in this factory class: the first part is the abstraction of the abstract factory class inherited. Method, in this method, a specific DAO object is created (these objects of these objects are implemented in the 4th unfolded definition), and three methods in this article have created three specific DAO objects, of course, in order to achieve detail hidden, these methods return These specific DAO type doors implementation interface (these interfaces are implemented in step 3).
3 Define the interface of the specific DAO class and define all business methods, and data operation methods in the interface.
4 Define specific DAO classes, in this class is the actual business method, and the implementation of the data operation.
5 Define data transfer objects, he is used to pass data between the client and DAO, he is actually a JavaBean.
6 After completing the above 5, we can use the data client to define all classes by the DAO design pattern (see the last code example block).
With more than 6 steps, you need to experience the time when programming, in general, a table in the database can correspond to a data transfer class is the class defined in step 4, the properties in the class are the fields in the table. Then add the corresponding GET, SET method. Then press the mode and the above steps to define the specific class.
This article is translated from:
Http://java.sun.com/blueprints/corej2eepatterns/patterns/dataaccessObject.html