Thursday, April 12, 2012

Design Patterns (JAVA)

Singleton - A singleton is an object that cannot be instantiated.

pattern:
 
public class SingletonObject
{
    private SingletonObject(){}; // Private constructor so the object cannot be 
                                 // instantiated outside of this class

    public static SingletonObject getSingletonObject() // Allow a method to create a 
    {                                                  // local copy if the object is
      if (ref == null)                                 // not yet created. And return
          // it's ok, we can call this constructor     // that object
          ref = new SingletonObject();
      return ref;
    }

    private static SingletonObject ref;                // provide a local reference
}


source: http://www.javacoffeebreak.com/articles/designpatterns/index.html