Recently, I participated in a web programming project using the Struts framework and decided to adopt the DAO design mode when processing data persistence. Therefore, I read the DAO design mode in Sun's core J2EE design mode, part of the translation is shared by everyone. I hope you can point out the shortcomings and errors. I hope this article will help you.
When programming in Java, sometimes it seems very straightforward to implement but it has to use the design pattern to turn several turns to implement it. This seems to be a lot more, but some mature design pattern is adopted, makeProgramMore robust, loosely coupled, and well maintained and scalable.
Implement DAO design mode
Implement factory policies for Dao
1 adopt the factory method design mode
If a DaO factory is only implemented for a database (such as Oracle) and many Dao are created, we will consider adopting the factory method design mode when implementing this policy. assume that the factory class has created some objects such as customerdao, accountdao, and orderdao.
2. Use the abstract factory design model:
If we want to implement this policy for three different types of databases, we can consider adopting the abstract factory design model. suppose. this factory has created a series of Dao for customerdao, accountdao, and orderdao, which applies the implementation of factory methods in the factory class generated in the abstract factory.
CodeNote:
The following code illustrates the specific implementation of the DAO design mode:
Taking the abstract factory design pattern as an example to deal with multiple types of databases, the following example only lists the specific implementation of the DAO design pattern of the cloudscape database type, the implementation of DAO design patterns for other types of databases is 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 concrete factories will have
// Implement these methods.
// All the methods necessary to implement the factory class of the abstract factory. Use these methods to create a specific DAO class.
Public abstract customerdao getcustomerdao ();
Public abstract accountdao getaccountdao ();
Public abstract orderdao getorderdao ();
// Use the static method of this abstract class to create other specific Dao factory classes
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. The following is the implementation of the cloudscape Dao factory class, which implements the connection to this type of database and the methods that must be implemented in the abstract factory class that he inherits, create a specific Dao object in these methods.
// 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
// Establish a cloudscape connection
Public static connection createconnection (){
// Use driver and dburl to create a connection
// Recommend connection pool Implementation/usage
}
// Create a customerdao object. Of course, the returned interface is implemented by this class. Its advantage is that the implementation details are concealed.
Public customerdao getcustomerdao (){
// Cloudscapecustomerdao implements customerdao
Return new cloudscapecustomerdao ();
}
// Create an accountdao object. Of course, an interface implemented by this class is returned. Its advantage is that the implementation details are concealed.
Public accountdao getaccountdao (){
// Cloudscapeaccountdao implements accountdao
Return new cloudscapeaccountdao ();
}
// Create an orderdao object. Of course, an interface implemented by this class is returned. Its advantage is that the implementation details are concealed.
Public orderdao getorderdao (){
// Cloudscapeorderdao implements orderdao
Return new cloudscapeorderdao ();
}
...
}
3. the following code is the specific DAO class implementation interface, that is, the interface implemented by cloudscapecustomerdao (): customerdao. All business methods are defined in this interface.
// Interface that all mermerdaos 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 cloudscapecustomerdao class are concealed from the customer data end.
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
// Supplied criteria.
// Return a rowset.
}
Public Collection selectcustomersto (...){
// Implement search customers here using
// Supplied criteria.
// Alternatively, implement to return a collection
// Of transfer objects.
}
...
}
5. The following code transfers data from the data client to Dao. It is actually a JavaBean;
Public Class Customer implements java. Io. serializable {
// Member variables
Int customernumber;
String name;
String streetaddress;
String city;
...
// Getter and setter methods...
...
}
6. Finally, the application of the customer data end 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 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
// Get values.
Briefly speaking, follow these six steps to implement the mode:
1. Create an abstract factory class. It consists of two important parts: The first part is some abstract methods which must be implemented by all the specific factory classes that implement the abstract factory. the second part is a static method, which creates a factory object for a specific type of data source, such as cloudscapedaofactory () in the text ().
2. Create the factory classes for each data source respectively (this article takes cloudscapedaofactory as an example ). there are also two important components in this factory class: The first part is to implement the Left and Right abstract methods in the abstract factory class that he inherits, create a specific Dao object in this method (the classes of these objects are not defined in 4th). In this article, three methods create three specific Dao objects respectively, of course, to conceal the implementation details, these methods return the interfaces implemented by these specific Dao classes (these interfaces are implemented in step 1 ).
3. define the specific Dao interface and define all business methods and data operation methods in the interface.
4. define a specific DAO class. In this class, the actual business methods and data operations are implemented.
5. Define a data transmission object. It is used to transmit data between the client and Dao. It is actually a JavaBean.
6. After completing the above five steps, we can use the classes defined above by the DAO design mode on the Data Client (see the last code sample block ).
In the above six steps, you need to have a specific experience in programming. Generally, a table in the database can correspond to a data transmission class, that is, the class defined in step 1, the attribute in the class is the field in the table, and then add the corresponding get and set methods. then define the specific class according to the mode and the above steps.