/ Published in: ActionScript 3
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package kc.core { import flash.errors.IllegalOperationError; import flash.utils.Dictionary; public class KCSingleton extends Object { // @const public static const ERROR:String = "Illegal instantiation attempted on class of singleton type."; // @private private static var _records:Dictionary; // @constructor public function KCSingleton() { throw new IllegalOperationError( ERROR ); } // @methods public static function register( api:String, value:Class ):void { if( _records == null ) { _records = new Dictionary(true); } if( ! KCSingleton.getClass( api ) ) { _records[api] = value; } } public static function getClass( api:String ):Class { if( _records == null ){ throw new IllegalOperationError("No records."); } return _records[api]; } public static function getInstance( api:String ):* { var instance:Class = KCSingleton.getClass(api); if( ! instance ){ throw new Error("No class registered for interface \""+api+"\"."); } return instance["getInstance"](); } public static function purge(...rest):void { _records = null; } } }
URL: http://www.kirikacode.com