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

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.)

No comments: