我用工厂方法创建了一个DAO类,特定的DAO返回单例,即DAO的单个实例。但是我一直在追踪它并且正在创建但是我试着调用它并且它总是为空。
只是解释存储工厂
我在DAOFactory上调用RAMDAOFactory来获取RAMUserDAO 如果有更好的方法来处理RAM,序列化和SQL类型DAO或CRUD,请告诉我。
我正在调用存储的类。
public class Registration
{
private UserDAO userStorage;
private static Logger log = LogClass.getLog();
Registration(DAOFactoryType typeDataStorage)
{
userStorage = DAOFactory.getDAOFactory(typeDataStorage).getUserDAO();
log.trace("insdie Reg");
}
void addUser(String userName, String password, UserType... args)
throws Exception
{
List<UserType> userTypes = new ArrayList<UserType>(args.length);
for (UserType userType : args)
{
log.trace("userType " + userType);
userTypes.add(userType);
}
User newUser = new DefaultUser(userName, password, userTypes);
log.trace("newUser " + newUser);
if (userStorage != null)
{
userStorage.insert(newUser);
}
else
{
log.trace("userStorage null");
}
}
}
这是我的DAOFactory
public abstract class DAOFactory
{
private static Logger log = LogClass.getLog();
public abstract TradeDAO getTradeDAO();
public abstract UserDAO getUserDAO();
public abstract LogDAO getLogDAO();
public static DAOFactory getDAOFactory(DAOFactoryType factoryType)
{
switch (factoryType)
{
case SQL:
return new SQLDAOFactory();
case RAM:
log.trace("insdie RAM");
return new RAMDAOFactory();
case SERIAL:
return new SerialDAOFactory();
default:
return null;
}
}
}
RAMDAOFactory
public class RAMDAOFactory extends DAOFactory
{
private static Logger log = LogClass.getLog();
private TradeDAO ramTradeDAO;
private UserDAO ramUserDAO;
private LogDAO ramLogDAO;
public RAMDAOFactory()
{
log.trace("insdie RAMDAOFactory");
RAMUserDAO.getRAMUserDAO();
RAMTradeDAO.getRAMTradeDAO();
RAMLogDAO.getRAMLogDAO();
}
@Override
public TradeDAO getTradeDAO()
{
return ramTradeDAO;
}
@Override
public UserDAO getUserDAO()
{
return ramUserDAO;
}
@Override
public LogDAO getLogDAO()
{
return ramLogDAO;
}
}
这是我的UserDAO
public class RAMUserDAO implements UserDAO
{
/*
* Map<Integer, List<byte[]>> userHash; List<byte[]> arrayHashSalt;
*/
private static RAMUserDAO userDAO = null;
private Map<String, User> userList;
private static Logger log = LogClass.getLog();
private RAMUserDAO()
{
userList = new HashMap<String, User>();
log.trace("insdie RAMUserDAO constructor");
}
public static RAMUserDAO getRAMUserDAO()
{
log.trace("insdie getRAMUserDAO");
if(userDAO == null) {
log.trace("insdie new RAMUserDAO()");
userDAO = new RAMUserDAO();
}
/*if (userDAO == null)
{
synchronized (RAMUserDAO.class)
{
if (userDAO == null)
{
userDAO = new RAMUserDAO();
}
}
}*/
return userDAO;
}
@Override
public void insert(User user) throws Exception
{
log.trace("insdie insert");
userList.put(user.getUserName(), user);
}
}
监督是在RAMDAOFactory,修复是:
public class RAMDAOFactory extends DAOFactory
{
private static Logger log = LogClass.getLog();
@Override
public TradeDAO getTradeDAO()
{
return RAMTradeDAO.getRAMTradeDAO();
}
@Override
public UserDAO getUserDAO()
{
return RAMUserDAO.getRAMUserDAO();
}
@Override
public LogDAO getLogDAO()
{
return RAMLogDAO.getRAMLogDAO();
}
}
答案 0 :(得分:0)
你已经调用了方法
public RAMDAOFactory()
{
log.trace("insdie RAMDAOFactory");
RAMUserDAO.getRAMUserDAO();
RAMTradeDAO.getRAMTradeDAO();
RAMLogDAO.getRAMLogDAO();
}
但您尚未将其值分配给任何内容
@Override
public UserDAO getUserDAO()
{
return ramUserDAO;
}
要么总是打电话
RAMUserDao.getRAMUserDAO();
当您想要返回UserDAO
或将其分配给ramUserDAO
并返回时。