SingletonBase is the fundamental underlying class for the Anonymous MVC framework. All of the major support components of the framework extending this class. In this implementation, a single instance is spawned that parents all sub-class children. By adopting this approach items such as the Controller can register without conflict with co-existing Controllers from other parts of the application when used in Modular development. This allows applications to be sub-divided and developed independently without fear of naming conflict as singletons are recorded by fully qualified Class. This leaves centralization of functionality as an option to the development and not a requirement.

/*
 * SingletonBase.as
 *
 * Copyleft(c) 2010 Darin Kohles.  All Rights Reserved.
 *                        www.dkohles.com
 *
 * Date: April 10, 2010
 * Author: www.dkohles.com
 *
 */
package com.dkohles.core.base
{
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;

	/**
	 * Base class implementation of Singleton pattern. Single static storage for
	 * extending Class instances are stored via Dictionary.
	 *
	 */
	public class SingletonBase extends EventDispatcher
	{
		private static var instanceDict:Dictionary = new Dictionary();

		/**
		 * Public constructor. Never called directly, only through a reference call to super()
		 * from an extending sub class.
		 *
		 */
		public function SingletonBase()
		{
			var className:String = getQualifiedClassName( this );
			var clss:Class = getDefinitionByName( className ) as Class;

			if( clss == SingletonBase ){
				throw("SingletonBase is a base class that cannot be instantiated");
			}

			var instance:Object = instanceDict[clss];
			if( instance != null ){
				throw("Classes extending SingletonBase can only be instantiated once by the getInstance method");
			}else{
				instanceDict[clss] = this;
			}
		}

		public static function getInstance( clss:Class ):Object
		{
			var instance:Object = instanceDict[clss];

			if( instance == null ){
				// create a new instance of the given class
				instance = Object( new clss() );

				var singleton:SingletonBase = instance as SingletonBase;
				if( singleton == null ){
					throw("getInstance can only be called for Classes extending SingletonBase");
				}
			}

			return instance;
		}
	}
}

Please notice that the singleton base extends EventDispatcher. This allows instances to act as event capture points, which will be further illustrated in future posts.

Anonymous MVC

October 15

Since I haven’t blown the dust off my blog in some time (cough). I thought I’d finally put out access to and a description of a framework I’ve been working on. The basic motivation was implementation of a basic messaging framework that is simpler and more loosely coupled than Cairngorm. The result utilizes Singletons as the basis for Command, Control and Service (as well as Model if desired) nodes that act without specific knowledge of the View components that will utilize their functionality. By decoupling this functionality, development work on projects with broad scope, and many developers can proceed very easily. Each individual contributors work can be instantly integrated.

I hope this teaser will have you checking back soon, as I will start with the basics, including code. This framework can easily be scaled from small projects to large, which I will explain in future posts.