Promemoria: singleton

http://en.wikipedia.org/wiki/Singleton_pattern 
public class Singleton {
// Costruttore privato per impedirne l'uso pubblico
private Singleton() {}
/**    
* SingletonHolder viene letta alla prima esecuzione di 
* Singleton.getInstance()     
* oppure al primo accessi di SingletonHolder.INSTANCE, non prima.    
*/
private static class SingletonHolder
{
	private final static Singleton INSTANCE = new Singleton();
}

public static Singleton getInstance()   {
	return SingletonHolder.INSTANCE;
}
}
Oppure:
public class Singleton {
	private final static Singleton INSTANCE = new Singleton();
	// Costruttore privato per impedirne l'uso pubblico
	private Singleton() {}
	public static Singleton getInstance()
	{
		return INSTANCE;
	}
}

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>