// shall I write some keywords here to boost search engine ranking?

Showing posts with label idea. Show all posts
Showing posts with label idea. Show all posts

Wednesday, August 10, 2005

Another Simple DAO (cont')

Once we build these bases classes and interfaces. We may start implement it. First, is to implement the DAO interface. In my implementation, I create one more layer of abstraction before going in to the concrete implementation that is storage medium dependent.

So I create an interface that implement (with keyword extends) DAO interface, said CustomerDAO. My CustomerDAO are as below (Customer here is just a JavaBean):

public interface CustomerDAO extends DAO{
public Customer getCustomer (int id);
public void addCustomer (Customer customer);
public void updateCustomer (Customer customer);
public void deleteCustomer (Customer customer);
}
Then I implement the CustomerDAO interface into concrete implementation CustomerMySqlDAO. This class provide implementation that is specific to MySQL database and of course it is optimize for MySQL since it is not meant to be generic across DBMS or other storage medium.

After DAO is build, I also build my DAOFactory implementor, CustomerDAOFactory. The getDAO( ) method are as follow:


public DAO getDAO() {
ReceipientDAO dao = new ReceipientMySqlDAO();
return dao;
}
Until here, everything is ready for DAO for Customer object. You can use them in your Java program easily. Here a simple example:
public static  void main(String args[]) throws DAOException{
DAOFactory daoFactory = DAOFactory.getInstance("CustomerDAOFactory");
CustomerDAO dao = (CustomerDAO) daoFactory.getDAO();

Customer c = dao.getCustomer(2); // get customer by ID
System.out.println("Customer Name: " + c.getName());
}
Since down-casting is dangerous (it may throws ClassCastException at run time), and also create overhead. I am still looking for an alternative to avoid down-casting in my DAO implementation. If you have any idea for this, just post it here to share with others.

Monday, August 08, 2005

Another Simple DAO (cont')

Before, I continue on how to implement these 3 based classes in ur , let me first show you how the getInstance() code that utilise reflction will look like look like.


public static DAOFactory getInstance(String className)
throws DAOException{
try{
Class clsDAOFactory = Class.forName(className);
DAOFactory daoFactory = (DAOFactory) clsDAOFactory.newInstance();

return daoFactory;
}catch(ClassNotFoundException cnfe){
throw new DAOException(cnfe.getMessage());
}catch(InstantiationException ie){
throw new DAOException(ie.getMessage());
}catch(IllegalAccessException iae){
throw new DAOException(iae.getMessage());
}
}

The className in the code refer to the class name of your concrete implementation of DAO Factory.

As you can see, Java reflection can be very simple and yet powerful. It is a good investment to spend some time to explore this.

Why I do not show the code of the getDAO() method as well? Good question, because that is just a abstract method that to be implement by yourself. (Sorry for my simple class diagram do not show that.)

Friday, August 05, 2005

Another Simple DAO

DAO is one of the J2EE Patterns that aim to abtract the data access layer from business logic. Explaination from Java Sun Website (Core J2EE Patterns - Data Access Object) has show 2 effective patterns for DAO creation, FactoryMethod and Abstract Factory.

However, as shown in the class diagram, both patterns instantiate each Class of DAO using different methods. It means that each time your system need a new DAO for new type of data, the Factory need to be modified by adding a new method.

This seem a bit tedious for a lazy people like me. So I have come out an idea off utilising Java reflection to overcome this. Once I try to code it, I add 1 interface and 1 custom Exception class into the same package. Here is the simple class diagram:



DAOFactory is a abstract class that u only can get the instance of it through getInstance() method. However, it is not a Singletons. It return a new instance of the underlying DAOFactory implementor for each call.

DAO interface will be just a empty interface, while DAOException will just inherit everything from java.lang.Exception.

My implementation still have some limitations. I will try to overcome them before futher explain it. However, my main idea of implementating reflection to avoid adding new method for each DAO is already there. I hope you may try to extend this concept and share your implementation with me. Perhaps you may just solve the problems I face now.