DAO factories
In order to be able to select in a simple way among the
different DAO implementations that we may end up with, we'll
need a "DAO factory", another popular design pattern.
There's a choice here when implementing such a factory. You
could code a single factory which takes some input parameter
which specifies the DAO implementation to use. Or you could code
an interface which a factory must implement, and then code
several factories. I'll use the latter alternative:
package dk.hansen;
/*
* This a factory for the DVDManager DAO
*/
public interface DVDManagerFactoryIF {
public DVDManagerIF createDVDManager();
}
Usually a factory will be able to create several DAOs, but in
our simple example there's only the DVDManager DAO.
A factory class for the SimpleDVDManager can now be
coded:
package dk.hansen;
import org.apache.log4j.Logger;
public class SimpleDVDManagerFactory implements DVDManagerFactoryIF {
private static Logger logger =
Logger.getLogger(SimpleDVDManagerFactory.class.getName());
public DVDManagerIF createDVDManager() {
logger.info("SimpleDVDManagerFactory creates a SimpleDVDManager");
return new SimpleDVDManager();
}
}
Since I've promised not to skip proper logging and error
handling I don't use System.out.println for output,
but Log4J.
The relations between the classes are now:
Unit testing the code
At this stage we should make a unit test program for the
factory and DAO interface.
Here's a
JUnit test program that uses the
SimpleDVDManager and its factory class. It
tests all the methods in the DAO interface.
The factory is selected by giving the name of the class
as a string:
. . .
// Create a factory
String managerName = "dk.hansen.SimpleDVDManagerFactory";
DVDManagerFactory factory;
factory = (DVDManagerFactory)Class.forName(managerName).newInstance();
// Create a DVDManager
manager = factory.createDVDManager();
. . .
The application which will use the DAO can also instantiate the
factory by first reading the class name from--for example--a
properties file, through JNDI, or some other source. Test
programs for the application can therefore easily select simpler
DAO implementations than for example the database implementation
to be used in production. If new factories are developed,
they're also simple to plug in.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.