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

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.

No comments: