How to use mx.core.Singleton (Flex2.0.1 Flex3)

mx.core.Singleton is internal class in FlexFramework. I don't know Adobe supports mx.core.Singleton officially or not. Also FlexFrameworks's Singleton isn't like Java's because AS3 doesn't support Singleton. Also you don't need to use Singleton. I just show a way to use mx.core.Singleton.


SingletonSampleForFlex2.mxml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application layout="vertical"  
  3.    xmlns:mx="http://www.adobe.com/2006/mxml"  
  4.    viewSourceURL="srcview/index.html">  
  5.   
  6.   <mx:Script>  
  7.     <![CDATA[  
  8.       import mx.core.Singleton;  
  9.   
  10.       import org.pepe.managers.IFooManager;  
  11.       import org.pepe.managers.IHogeManager;  
  12.         
  13.       private function helloHandler():void  
  14.       {  
  15.         var cName:String = "org.pepe.managers::HogeManager";  
  16.           
  17.         // This doesn't need.  
  18.         // But you can check which Class is registered or not  
  19.         var c:Class = Singleton.getClass(cName);  
  20.           
  21.         // get instance  
  22.         var hoge:IHogeManager =  
  23.           Singleton.getInstance(cName) as IHogeManager;  
  24.             
  25.         hoge.sayHello();  
  26.       }  
  27.         
  28.       private function goodByeHandler():void  
  29.       {  
  30.         var cName:String = "org.pepe.managers::FooManager";  
  31.           
  32.         // This doesn't need.  
  33.         // But you can check which Class is registered or not  
  34.         var c:Class = Singleton.getClass(cName);  
  35.           
  36.         // get instance  
  37.         var foo:IFooManager =  
  38.           Singleton.getInstance(cName) as IFooManager;  
  39.             
  40.         foo.sayGoodBye();  
  41.       }  
  42.         
  43.     ]]>  
  44.   </mx:Script>  
  45.     
  46.   <mx:Label text="Singleton Sample for Flex 2.0.1"  
  47.      fontSize="26" fontWeight="bold" />  
  48.     
  49.   <mx:Spacer height="20" />  
  50.     
  51.   <mx:HBox>  
  52.     <mx:Button label="Hello" click="helloHandler()" />  
  53.     <mx:Button label="GoodBye" click="goodByeHandler()" />  
  54.   </mx:HBox>  
  55.     
  56. </mx:Application>  



You have to register classes to Singleton before you use it. About Mixin metadata, See The [Transient] and [Mixin] metadata tags.
org.pepe.managers.AppBootstrap (AS3)

  1. package org.pepe.managers  
  2. {  
  3.   import flash.utils.getDefinitionByName;  
  4.     
  5.   import mx.core.Singleton;  
  6.   import mx.managers.ISystemManager;  
  7.   import mx.utils.ObjectUtil;  
  8.     
  9.   [Mixin]  
  10.   public class AppBootstrap  
  11.   {  
  12.   
  13.     // SystemManager call this function  
  14.     // before Application initialize  
  15.         public static function init(sm:ISystemManager):void  
  16.         {  
  17.           // set a break point here and try to debug:)  
  18.           trace(ObjectUtil.toString(sm.info()));  
  19.             
  20.           // register Class info to Singleton class  
  21.             Singleton.registerClass(  
  22.               "org.pepe.managers::HogeManager",  
  23.         Class(getDefinitionByName("org.pepe.managers::HogeManager"))  
  24.         );  
  25.             Singleton.registerClass(  
  26.               "org.pepe.managers::FooManager",  
  27.         Class(getDefinitionByName("org.pepe.managers::FooManager"))  
  28.         );  
  29.         }  
  30.   
  31.   
  32.     private var _hoge:HogeManager;  // for compiler  
  33.     private var _foo:FooManager;  // for compiler  
  34.   
  35.     public function AppBootstrap()  
  36.     {  
  37.       super();  
  38.     }  
  39.   
  40.   }  
  41. }  



org.pepe.managers.HogeManager (AS3)

  1. package org.pepe.managers  
  2. {  
  3.   import flash.events.EventDispatcher;  
  4.   import flash.events.IEventDispatcher;  
  5.     
  6.   import mx.controls.Alert;  
  7.   
  8.   public class FooManager extends EventDispatcher implements IFooManager  
  9.   {  
  10.     private static var _instance:FooManager;  
  11.       
  12.     // Singleton call this function  
  13.     public static function getInstance():FooManager  
  14.     {  
  15.       if(!_instance){  
  16.         _instance = new FooManager();  
  17.       }  
  18.         
  19.       return _instance;  
  20.     }  
  21.   
  22.   
  23.     public function FooManager(target:IEventDispatcher=null)  
  24.     {  
  25.       super(target);  
  26.     }  
  27.       
  28.     public function sayGoodBye():void  
  29.     {  
  30.       Alert.show("GoodBye""FooManager");  
  31.     }  
  32.       
  33.   }  
  34. }  



You have to add compiler option like below.
"-load-config+=config/custom-config.xml"

custom-config.xml

<?xml version="1.0"?>
<flex-config xmlns="http://www.adobe.com/2006/flex-config">
   
      <symbol>org.pepe.managers.AppBootstrap</symbol>
   </includes>
</flex-config>



SingletonSampleForFlex2
source of SingletonSampleForFlex2


SingletonSampleForFlex3
source of SingletonSampleForFlex3


BTW, on Japanese environment, css definition of source view is output in Japanese. Therefore, source view doesn't work correctly.

Share