Return to Snippet

Revision: 21756
at December 20, 2009 02:39 by alejandromb


Updated Code
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;
		}
		
	}
	
}

Revision: 21755
at December 20, 2009 02:38 by alejandromb


Initial Code
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 {
			KCPurger.purgeCollection( _records );
			_records = null;
		}
		
	}
	
}

Initial URL
http://www.kirikacode.com

Initial Description

                                

Initial Title
AS3 | KCSingleton

Initial Tags

                                

Initial Language
ActionScript 3