Library Class

June 12th, 2008

This Library Class represents a standard API for dealing with the Dictionary Class. I tend to have classes all over the place that store items in an Associative Arrays, or in Dictionaries. I found that a lot of these classes always do the same thing but I was constantly writing unique names for adding and retrieving content. This class is an attempt to consolidate, and simplify this design model.

The library is very simple, elegant really, and when I show you what I have done you will probably wonder why you haven’t set one of these up already. For those of you who have already been doing something like this I am interested in knowing how your class differs from mine.

The first part of this class is a Interface for the library. The interface is important as you will see later in the post when I talk about how to create a Singleton Library Class.

package
{
        import flash.utils.Dictionary;
       
        public interface ILibrary
        {
                function get data():Dictionary;
               
                function load(name:String, data_url:String) : *;

                function getItem(name:String) : *;
               
                function setData(name:String, data:*):*;
               
                function register(name:String, item:*):*;
               
                function clone():Library;
        }
}

And here is the meat and potatoes of the Library Class.

package
{
       
        import flash.events.Event;
        import flash.utils.Dictionary;
       
        public class Library implements ILibrary
        {
               
                public static const INIT:String = “init”;
                private var _library:Dictionary;
               
                /**
                 * Return’s data from the Dictionary.
                 */

                 public function get data():Dictionary{
                        return _library;       
                 }
                
                /**
                 *
                 */

                public function Library(data:Dictionary = null)
                {
                        if(data)
                                _library = data;
                        else
                                _library = new Dictionary();
                }
               
                /**
                 *
                 */

                public function load(name:String, data_url:String):*{
                        // Use this to load in any data set and have its corresponding class get saved into the library.
                }
               
                /**
                 * Called when the data being requested has fully loaded.
                 */

                protected function onLoad(event:Event):void{
                        //Fire off Event here
                }
               
                /**
                 * Used to pass data directly into the library.
                 */

                public function setData(name:String, data:*):*{
                        // Register
                        register(name,data);
                }
               
                /**
                 * Gets item by name from library.
                 */

                public function getItem(name:String):*{
                        return _library[name];
                }
               
                /**
                 * Use this to register an item in the library that was created externally.
                 */

                public function register(name:String, item:*):*{

                        _library[name] = item;
                       
                        return _library[name];
                }
               
                /**
                 *
                 */

                 public function clone():Library{
                        return new Library(_library);
                 }
        }
}

So this is really straight forward class. It allows you to add in logic for loading in data externally or directly adding objects to the library. So here is an example of how to use this class:

// Create test data
var obj1:Object = new Object();
        obj1.name = “Object 1″;

var obj2:Object = new Object();
        obj2.name = “Object 2″;

// Create new library
var lib:Library = new Library();

// Add in data
lib.register(“ItemA”, obj1);
lib.register(“ItemB”, obj2);

// Get data - will trace out "Object 1"
trace(lib.getItem(“ItemA”).name);
// Get data - will trace out "Object 2"
trace(lib.getItem(“ItemB”).name);

Really simple right? Well what happens when you want a Singleton Library? You can use composition in order to create a single instance of the Library. Lets take a look:

package
{

        import flash.events.*;
        import flash.utils.Dictionary;
       
        public class SingletonLibrary extends EventDispatcher implements ILibrary
        {
               
                private static var __instance:SingletonLibrary;
                public var _library:Library;
               
                /**
                 *
                 */

                public static function get instance ():SingletonLibrary{
                        if(SingletonLibrary.__instance == null) {
                                SingletonLibrary.__instance = new SingletonLibrary(new SingletonEnforcer());
                        }
                        return SingletonLibrary.__instance;
                }
               
                /**
                 *
                 */

                public function get data():Dictionary{
                        return _library.data;   
                }
               
                /**
                 *
                 */

                public function SingletonLibrary(enforcer:SingletonEnforcer) {
                        _library = new Library();
                }
               
                /**
                 *
                 */

                public function load(name:String, data_url:String):*{
                        // Custom Load code
                }
               
                /**
                 *
                 */

                internal function loaded(event:Event):void{
                        // Stop Event
                        event.stopImmediatePropagation();
                }
               
                /**
                 *
                 */

                public function register(name:String, item:*):*{
                        return _library.register(name, item);
                }
               
                /**
                 *
                 */

                public function setData(name:String, data:*):*{
                        _library.setData(name, data);
                }
               
                /**
                 *
                 */

                public function getItem(name:String):*{
                        return _library.getItem(name);
                }
               
                /**
                 *
                 */

                public function clone():Library{
                        return _library.clone();
                }
               
        }
}

internal class SingletonEnforcer {}

So that’s basically it. There are a lot of powerful things you can do with a class like this. As I start to post more classes from my Sumo Framework you will see how important this single class happens to be.

Leave a Reply