View Single Post
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator 17 Creative Assets 2007-10-31 #7 Old  
Yeah, a singleton would be better IMO. They pretty much behave the same way as all-static classes do. I think the only time all-static classes are really needed is for utility classes and classes that simply contain parameter values (i.e. StageAlign and StageScaleMode etc).

It sounds like you are already familiar with singletons but I thought I would post the following code anyway. This is how I do things:

ActionScript Code:
  1. package
  2. {
  3.     /**
  4.      */
  5.     public class MySingleton extends EventDispatcher
  6.     {
  7.         //----------------------------------------------------------------------
  8.         //
  9.         // Private Properties
  10.         //
  11.         //----------------------------------------------------------------------
  12.        
  13.         /**
  14.          */
  15.         private static var singletonInstance:MySingleton = null;
  16.        
  17.         /**
  18.          */
  19.         private static var constructorLocked:Boolean = false;
  20.        
  21.         //----------------------------------------------------------------------
  22.         //
  23.         // Constructor
  24.         //
  25.         //----------------------------------------------------------------------
  26.        
  27.         /**
  28.          */
  29.         public function MySingleton()
  30.         {
  31.             if( constructorLocked )
  32.             {
  33.                 throw new Error( "MySingleton is a singleton" );
  34.             }
  35.         }
  36.  
  37.         //----------------------------------------------------------------------
  38.         //
  39.         // Public Methods
  40.         //
  41.         //----------------------------------------------------------------------
  42.  
  43.         /**
  44.          */
  45.         public function login( username:String, password:String ):void
  46.         {}
  47.        
  48.         //----------------------------------------------------------------------
  49.         //
  50.         // Getters/Setters
  51.         //
  52.         //----------------------------------------------------------------------
  53.        
  54.         /**
  55.          */
  56.         public static function get instance():MySingleton // read-only
  57.         {
  58.             if( singletonInstance == null )
  59.             {
  60.                 constructorLocked = false;
  61.                 singletonInstance = new MySingleton();
  62.                 constructorLocked = true;
  63.             }
  64.            
  65.             return singletonInstance;
  66.         }
  67.  
  68.     }
  69. }
The unwritten rules of OOP say the class instance should be accessed via a getInstance() method but I find using a getter makes the code a lot cleaner:

ActionScript Code:
  1. MySingleton.instance.login( ... );
Anyway, there you have it.

Reply With Quote