patternjavaMinor
Necessary singleton in DAO
Viewed 0 times
daonecessarysingleton
Problem
I am using a singleton in my DAO. Would I be better off with
static methods? Also, should I be worried about synchronization of the singleton and my data structures?public class RAMUserDAO implements GenericDAO
{
private static RAMUserDAO userDAO = null;
private Map userList;
private Map banList;
private static Logger log = LOG.PRODUCTION;
private RAMUserDAO()
{
userList = new ConcurrentHashMap();
banList = new ConcurrentHashMap();
}
public static RAMUserDAO getRAMUserDAO()
{
if (userDAO == null)
{
synchronized (RAMUserDAO.class)
{
if (userDAO == null)
{
userDAO = new RAMUserDAO();
}
}
}
return userDAO;
}
}Solution
Your spacing is all over the place, and the double null check against
I'm not making a comment on the big picture on whether or not the singleton is necessary, but I'd certainly fix
The Egyptian-style braces are just my general preference. I don't know if there's a Java standard for how to do the braces.
userDAO seems redundant.I'm not making a comment on the big picture on whether or not the singleton is necessary, but I'd certainly fix
getRAMUserDAO() up to look more like this:public static RAMUserDAO getRAMUserDAO() {
synchronized (RAMUserDAO.class) {
if (userDAO == null) {
userDAO = new RAMUserDAO();
}
return userDAO;
}
}The Egyptian-style braces are just my general preference. I don't know if there's a Java standard for how to do the braces.
Code Snippets
public static RAMUserDAO getRAMUserDAO() {
synchronized (RAMUserDAO.class) {
if (userDAO == null) {
userDAO = new RAMUserDAO();
}
return userDAO;
}
}Context
StackExchange Code Review Q#59741, answer score: 2
Revisions (0)
No revisions yet.