Monday, August 2, 2010

package innerclasspackage;

/**
 * A utility class of which at most one instance
 * can exist per VM.
 *
 * Use Singleton.instance() to access this
 * instance.
 */
public class Singleton {
   /**
    * The constructor could be made private
    * to prevent others from instantiating this class.
    * But this would also make it impossible to
    * create instances of Singleton subclasses.
    */
   protected Singleton() {
     // ...
   }


   /**
    * A handle to the unique Singleton instance.
    */
   static private Singleton _instance = null;

   /**
    * @return The unique instance of this class.
    */
   static public Singleton instance() {
      if(null == _instance) {
         _instance = new Singleton();
      }
      return _instance;
   }



}

No comments:

Post a Comment