I have several utility classes who should only ever have one instance in memory, such as LogHelper, CacheHelper, etc. One instance to rule them all.
I've never implemented Singleton in Java before, so I wanted to post my first pass and see if anybody had major issues with this, or just helpful critiques. I want to make sure that my design will have the intended affect when I go to use these singleton classes throughout my codebase.
Singleton:
public class Singleton
{
private static Singleton instance;
protected Singleton()
{
super();
}
public static synchronized Singleton getInstance()
{
if(instance == null)
instance = instantiate();
return instance;
}
// Wanted to make this abstract and let subclasses @Override it,
// but combining 'abstract' and 'static' is illegal; makes sense!
protected static Singleton instantiate()
{
return new Singleton();
}
}
LogHelper (a singleton since all my classes will use it to write messages to the same destinations):
public class LogHelper extends Singleton
{
protected static LogHelper instantiate()
{
return new LogHelper(...blah...);
}
public LogHelper(...blah...)
{
// Initialize the singleton LogHelper
}
// More methods for logging, etc.
}
Here's how I'd like to use it throughout my codebase:
LogHelper logHelper = LogHelper.getInstance();
logHelper.logInfo("...");
What I'm worried about is that I may have inadvertently designed this wrong and heading down the wrong avenue. Can anybody weigh-in on my design?!?
Thanks!