W
Wendy S
When I wrote my data access layer, I followed the J2EE DAO Pattern, and so I
have
public abstract class DAOFactory
{ public abstract AddressDAO getAddressDAO();
public static DAOFactory getDAOFactory()
{ return new UniDataDAOFactory(); }
}
and
public class UniDataDAOFactory extends DAOFactory
{ public AddressDAO getAddressDAO()
{ return new UniDataAddressDAO(); }
}
But it has grown and grown, and I don't want to include all the DAO classes
in every project. (For example, the accounting reports webapp doesn't need
all the DAO classes for the fund raising system.) But it feels wrong to
leave those classes out of the .jar file when the DAOFactory refers to them.
I'm considering removing all of the getWhateverDAO() methods from the
factory, and just doing this:
public abstract Object getDAO( String daoType ) throws DAOException;
Then the caller is responsible for supplying the type of DAO he wants, and
for casting the resulting Object to the correct type. Since I am a
development team of one, I'd like some feedback on this before I make the
change.
So far, the implementation (in UniDataDAOFactory) looks like this. It
expects the caller to supply mp.ContactDAO if they want a
my.package.mp.UniDataContactDAO, thus the substring manipulation.
public Object getDAO( String daoType ) throws DAOException
{
try {
int pos = daoType.lastIndexOf( "." );
if ( pos > 0 ) {
String pkgName = daoType.substring( 0, pos );
String className = daoType.substring( pos + 1 );
String fqn = "my.package.data." + pkgName + ".UniData" +
className;
Class theClass = Class.forName( fqn );
return theClass.newInstance();
} else {
String fqn = "my.package.data.UniData" + daoType;
Class theClass = Class.forName( fqn );
return theClass.newInstance();
} } catch ( Exception ex ) { throw new DAOException( ex ); } }
Does this look reasonable? I realize it opens up the possibility of more
runtime errors, since you can mistype the 'daoType' parameter and the
compiler can't catch it. Is there anything else I should consider?
Thanks for your time,
Wendy S
have
public abstract class DAOFactory
{ public abstract AddressDAO getAddressDAO();
public static DAOFactory getDAOFactory()
{ return new UniDataDAOFactory(); }
}
and
public class UniDataDAOFactory extends DAOFactory
{ public AddressDAO getAddressDAO()
{ return new UniDataAddressDAO(); }
}
But it has grown and grown, and I don't want to include all the DAO classes
in every project. (For example, the accounting reports webapp doesn't need
all the DAO classes for the fund raising system.) But it feels wrong to
leave those classes out of the .jar file when the DAOFactory refers to them.
I'm considering removing all of the getWhateverDAO() methods from the
factory, and just doing this:
public abstract Object getDAO( String daoType ) throws DAOException;
Then the caller is responsible for supplying the type of DAO he wants, and
for casting the resulting Object to the correct type. Since I am a
development team of one, I'd like some feedback on this before I make the
change.
So far, the implementation (in UniDataDAOFactory) looks like this. It
expects the caller to supply mp.ContactDAO if they want a
my.package.mp.UniDataContactDAO, thus the substring manipulation.
public Object getDAO( String daoType ) throws DAOException
{
try {
int pos = daoType.lastIndexOf( "." );
if ( pos > 0 ) {
String pkgName = daoType.substring( 0, pos );
String className = daoType.substring( pos + 1 );
String fqn = "my.package.data." + pkgName + ".UniData" +
className;
Class theClass = Class.forName( fqn );
return theClass.newInstance();
} else {
String fqn = "my.package.data.UniData" + daoType;
Class theClass = Class.forName( fqn );
return theClass.newInstance();
} } catch ( Exception ex ) { throw new DAOException( ex ); } }
Does this look reasonable? I realize it opens up the possibility of more
runtime errors, since you can mistype the 'daoType' parameter and the
compiler can't catch it. Is there anything else I should consider?
Thanks for your time,
Wendy S