I'm trying to do three different type of DAOs with 4 subtypes of each. I have RAM, SQL and Serial with each one have four subtypes.
Is a singleton necessary with my implementation within the factory and more specifically with each DAO? Should I use static methods instead? Also I have two factories, is that necessary?
I might be missing the point here of the factory and implementing incorrectly? I only posted RAM and RAMUser
with RAM Factory and DAO factory, I figure that's all really need to be post.
//Abstract class DAO Factory
public abstract class DAOFactory
{
private static Logger log = LOG.PRODUCTION;
public abstract GenericDAO<Trade, String, Boolean> getTradeDAO();
public abstract GenericDAO<User, String, Boolean> getUserDAO();
public abstract GenericDAO<User, String, Boolean> getLogDAO();
public abstract GenericDAO<ServiceRequest, String, Boolean> getRequestDAO();
private static DAOFactoryType currentUsedStorage;
/**
* @return the currentUsedStorage
*/
public static DAOFactoryType getCurrentUsedStorage()
{
return currentUsedStorage;
}
/**
* @param currentUsedStorage
* the currentUsedStorage to set
*/
public void setCurrentUsedStorage(DAOFactoryType currentUsedStorage)
{
DAOFactory.currentUsedStorage = currentUsedStorage;
}
public static DAOFactory getDAOFactory(DAOFactoryType factoryType)
{
switch (factoryType)
{
case SQL :
currentUsedStorage = DAOFactoryType.SQL;
return new SQLDAOFactory();
case RAM :
currentUsedStorage = DAOFactoryType.RAM;
return new RAMDAOFactory();
case SERIAL :
currentUsedStorage = DAOFactoryType.SERIAL;
return new SerialDAOFactory();
default :
return null;
}
}
}
GenericDAO
public interface GenericDAO<U,S,B>
{
public boolean update(U user, B active) throws NullUserException,
UserExistException, NullListException, UserInstanceException,
ReceivedNullParameterException;
public boolean delete(U user, B active) throws NullUserException,
UserExistException, NullListException, UserInstanceException,
ReceivedNullParameterException;
public U findUserByUserName(S userName, B active)
throws NullUserException, UserExistException, NullListException,
UserInstanceException, ReceivedNullParameterException;
public U findUserByUser(U user, B active)
throws NullUserException, NullListException, UserInstanceException,
ReceivedNullParameterException;
public boolean insert(U user, B active)
throws ReceivedNullParameterException, UserExistException,
NullListException, UserInstanceException;
}
RAMDAOFactory
public class RAMDAOFactory extends DAOFactory
{
private static Logger log = LOG.PRODUCTION;
@Override
public GenericDAO<Trade, String, Boolean> getTradeDAO()
{
return RAMTradeDAO.getRAMTradeDAO();
}
@Override
public GenericDAO<User, String, Boolean> getUserDAO()
{
return RAMUserDAO.getRAMUserDAO();
}
@Override
public GenericDAO<User, String, Boolean> getLogDAO()
{
return RAMLogDAO.getRAMLogDAO();
}
@Override
public GenericDAO<ServiceRequest, String, Boolean> getRequestDAO()
{
return RAMServiceRequestDAO.getRAMRequestDAO();
}
}
RAMUserDAO
public class RAMUserDAO implements GenericDAO<User, String, Boolean>
{
private static RAMUserDAO userDAO = new RAMUserDAO();
private Map<String, User> userList;
private Map<String, User> banList;
private static Logger log = LOG.PRODUCTION;
private RAMUserDAO()
{
userList = new ConcurrentHashMap<String, User>();
banList = new ConcurrentHashMap<String, User>();
}
public static RAMUserDAO getRAMUserDAO()
{
return userDAO;
}
@Override
public boolean update(User user, Boolean active) throws NullUserException,
UserExistException, NullListException, UserInstanceException,
ReceivedNullParameterException
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean delete(User user, Boolean active) throws NullUserException,
UserExistException, NullListException, UserInstanceException,
ReceivedNullParameterException
{
// TODO Auto-generated method stub
return false;
}
@Override
public User findUserByUserName(String userName, Boolean active)
throws NullUserException, UserExistException, NullListException,
UserInstanceException, ReceivedNullParameterException
{
// TODO Auto-generated method stub
return null;
}
@Override
public User findUserByUser(User user, Boolean active)
throws NullUserException, NullListException, UserInstanceException,
ReceivedNullParameterException
{
// TODO Auto-generated method stub
return null;
}
@Override
public boolean insert(User user, Boolean active)
throws ReceivedNullParameterException, UserExistException,
NullListException, UserInstanceException
{
// TODO Auto-generated method stub
return false;
}
}
currentUsedStorage
is a bad idea. A global field which you can set, but which also gets set accidentally (getDAOFactory
), what can it be used for. Imagine a multithreaded program... \$\endgroup\$