Thursday, October 28, 2010

Singleton Pattern Implementation Template

The following is a Java Template to implement the Singleton pattern. 
 
public class SingletonObject
{
  private SingletonObject()
  {
    // no code req'd
  }

  public static SingletonObject getSingletonObject()
  {
    if (ref == null)
        // it's ok, we can call this constructor
        ref = new SingletonObject();  
    return ref;
  }

  public Object clone()
 throws CloneNotSupportedException
  {
    throw new CloneNotSupportedException(); 
    // that'll teach 'em
  }

  private static SingletonObject ref;
}
 
For more explanation visit: http://www.javacoffeebreak.com/articles/designpatterns/index.html 

No comments:

Post a Comment