Package | com.ericfeminella.utils |
Class | public final class SingletonManager |
Upon instantiation of a Singleton class, the class constructor
invokes SingletonManager.validateInstance
and simply
passes a reference to the instance.
SingletonManager.validateInstance( this );
The fully qualified class name is then used as the unique key in
the HashMap instance (singletons
). The class name /
key is then assigned a Boolean value of true. When a subsequent
instantiation of the Singleton class occurs the Singleton class
repeats the process of invoking SingletonManager.validateInstance
to see if the Singleton class has been mapped previously, and if
so an exception is thrown.
Classes must invoke SingletonManager.validateInstance
from within their constructor in order to garuntee they are only to
have single instance created, hence the term Singleton.
SingletonManager.validateInstance
in order to simplify
the Singleton construction process by delegating Singleton Management
to a specific object rather than the class implementation itself
package com.examples { public class SomeSingleton { // Defines the singleton instance of SomeSingleton public static const instance:SomeSingleton = new SomeSingleton(); // Singelton class passes in a reference of the instance, // SingletonManager determines the fully qualified name // of the class and validates the instance public function SomeSingleton() { SingletonManager.validateInstance( this ); } } }
The above example guarantees that only one instance of the example class "SomeSingleton" can ever be instantiated.
Method | Defined by | ||
---|---|---|---|
validateInstance(instance:*):void
[static]
Validates a new Singleton class instance to determine if the
class has previously been instantiated, if so, an Exception
is thrown, thus indicating that the class is a Singleton and
is only intended to have a single instance instantiated
| SingletonManager |
validateInstance | () | method |
public static function validateInstance(instance:*):void
Validates a new Singleton class instance to determine if the class has previously been instantiated, if so, an Exception is thrown, thus indicating that the class is a Singleton and is only intended to have a single instance instantiated
Parametersinstance:* — class instance which has been instantiated
|