Well, I assume you are talking about java. The java language itself does not provide a good way to define the Singleton pattern IMO. That is why when I want to use specific patterns in my code, such as that one, I extend it using AspectJ.
With AspectJ you can actually "catch" every call to any constructor and make sure that you are always returning the same object, thus having a singleton, no matter what.
In case you want it, here is the protocol code in AspectJ:
package singleton.aspectJ;
import java.util.Hashtable;
import java.util.Map;
import org.aspectj.lang.annotation.AdviceName;
/**
* Defines the general behavior of the Singleton design pattern.
*
* Each concrete sub-aspect of SingletonProtocol defines the Singleton
* property for one or more types.
*
* The sub-aspect defines two things: <ol>
*
* <li> what types are <i>Singleton</i> <br>
*
* <li> what classes can access the <i>Singleton</i>'s constructor (if any)
* despite its property
* </ol>
*
* for this implementation we choose to illustrate that it is not necessary
* to provide a factory method for accessing the <i>Singleton</i>
* instance (like <i>getSingleton()</i>). The regular
* constructor may be used instead.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @author Pedro Martins
* @version 1.1.1, 21/11/2011
*/
public abstract aspect SingletonProtocol {
/**
* stores the <i>Singleton</i> instances
*/
private Map<Class<Object>, Object> singletonsTable =
new Hashtable<Class<Object>, Object>();
/**
* Defines the <i>Singleton</i> role.
*/
protected interface SingletonInterface{}
/**
* Placeholder for exceptions to the <i>Singleton</i>'s constructor
* protection. For example, non-singleton subclasses may need to
* access the protected constructor of the <i>Singleton</i> normally.
*
* An alternative implementation would be to define an interface
* for singleton exceptions similar to the one above.
*/
protected pointcut protectionExclusions();
private pointcut singletonInstantiator() : call ((SingletonInterface+).new(..)) && !protectionExclusions();
/**
* Protects the <i>Singleton</i>'s constructor. Creates the unique
* instance on demand and returns it instead of a new object.
*
* @return the singleton instance
*/
@SuppressWarnings("unchecked")
@AdviceName("newCatcher")
Object around() : singletonInstantiator(){
Class<Object> singleton = thisJoinPoint.getSignature().getDeclaringType();
if(!singletonsTable.containsKey(singleton))
singletonsTable.put(singleton, proceed());
return singletonsTable.get(singleton);
}
}
And here is an example:
package singleton.aspectJ;
public aspect SingletonInstance extends SingletonProtocol{
declare parents: God implements SingletonInterface;
protected pointcut protectionExclusions():
call((Canaanites+).new(..));
}
In this case God is a singleton because it implements the SingletonInterface. Another class Cannnites is a child of God, but is added as an exception so it is not singleton.
Quite amazingly, this is the code of God and it's child:
public class God {
//Code specific to this class's objective
}
public class Caanites extends God {
//Code specific to this class's objective
}
As you can see, quite amazingly, the God and Caanites class don't have any pattern code.
I Hope I've help.