import java.util.Hashtable; /********************************************************************* * An example of the Singleton design pattern using a registry. *

* @author * David W. Croft * @version * 1998-04-07 *********************************************************************/ public class RegistrySingleton { ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// private static final Hashtable registry = new Hashtable ( ); private final String name; public static RegistrySingleton instance ( String name ) ////////////////////////////////////////////////////////////////////// { return ( RegistrySingleton ) registry.get ( name ); } protected RegistrySingleton ( String name ) ////////////////////////////////////////////////////////////////////// { this.name = name; if ( !register ( name, this ) ) { throw new IllegalArgumentException ( name + " already in the registry" ); } } /********************************************************************* * While all RegistrySingleton objects are usually registered * automatically when the constructor is called, it may be necessary * to register an instance that was revived from persistent storage. *********************************************************************/ protected static synchronized boolean register ( String name, RegistrySingleton registrySingleton ) ////////////////////////////////////////////////////////////////////// { if ( registry.get ( name ) != null ) return false; registry.put ( name, registrySingleton ); return true; } ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// }