Packagenet.guttershark.util
Classpublic final class Singleton

The Singleton class is a helper class to enforce singletons in Guttershark and enables subclassing of singletons.


Example
How singletons are implemented in guttershark:
 
  public class Model
  {
      private static var inst:Model;
      public function Model()
      {
          Singleton.assertSingle(Model);
      }
      public static function gi():Model
      {
          if(!inst) inst = Singleton.gi(Model);
          return inst;
      }
  }
  

The following example illustrates how you can extend Singletons, but it is not recommended; you should extend singletons through composition instead, which will always guarantee you don't run into any problems.

Extending singletons:
 
  //The base singleton class.
  package
  {
      public class MySingleton
      {
          private static var inst:MySingleton;
          public function MySingleton()
          {
              Singleton.assertSingle(MySingleton);
          }
          public static function gi():MySingleton
          {
              if(!inst) inst = Singleton.gi(MySingleton);
              return inst;
          }
      }
  }
  
  //the singleton extended from the base MySingleton class.
  package
  {
      import MySingleton;
      public class MyExtendedSingleton extends MySingleton
      {
          private static var inst:MyExtendedSingleton;
          public function MyExtendedSingleton()
          {
              Singleton.assertSingle(MyExtendedSingleton);
          }
          public static function gi():MyExtendedSingleton()
          {
              if(!inst) inst = Singleton.gi(MyExtendedSingleton,MySingleton);
              return inst;
          }
      }
  }
  
  package
  {
      public class Main extends Sprite
      {
          public function Main()
          {
              var mes:MyExtendedSingleton = MyExtendedSingleton.gi();
              //either of the following will fail.
              var ms:MySingleton = new MySingleton();
              var mss:MySingleton = MySingleton.gi();
          }
      }
  }
  



Public Methods
 MethodDefined by
  
assertSingle(clazz:Class):void
[static] Assert that there is only one instance of a class.
Singleton
  
gi(clazz:Class, ... cancelParentClasses):*
[static] Get an instance of a class, and cancel any parent classes.
Singleton
Method detail
assertSingle()method
public static function assertSingle(clazz:Class):void

Assert that there is only one instance of a class.

Parameters
clazz:Class — The Class to assert as being the only instance.
gi()method 
public static function gi(clazz:Class, ... cancelParentClasses):*

Get an instance of a class, and cancel any parent classes.

Parameters
clazz:Class — The class of the instance you're after.
 
... cancelParentClasses — An array of Classes to cancel, so they cannot be instantiated again. This is specifically for when you extend a singleton, you need to make sure you pass all of it's super classes.

Returns
*