Archive for September, 2007

MAX 2007 in Chicago

Sunday, September 30th, 2007

I arrived in Chicago earlier this afternoon as I will be attending MAX North America from Sunday - Wednesday.

This is my first time visiting the “Windy City” and it is a really nice place; very clean and very quite for such a large city, or maybe that’s just because I am used to NYC?

I am staying at the Hilton Towers and the food and drink at Kitty O’Sheas is worth the trip alone. The hotel also has a very interesting historical background as well.

If you should happen to be attending MAX as well feel free to look me up.

I am planning on writing a post soon after I return regarding what I feel were some of the more significant highlights of MAX 2007.

Enforcing an all static API in ActionScript 3

Friday, September 28th, 2007

It is quite common when designing an API or system in Adobe Flex that you will identify certain areas which call for specific classes to contain an all static API.

Typically, all-static classes are utilized as helper, utility and factory classes which provide static methods for performing common utility methods.

A commonly overlooked aspect of such designs is the assumption that an all-static class will not be misused by clients, specifically via instance instantiation, as it is assumed by the designer that an all static class would never be instantiated as there are no instance members available, only static class members. This is a fair assumption. However it is not possible to guarantee this will never occur as ActionScript 3 does not support private constructors (yes, I am back on this subject yet again). Although this is an unfortunate limitation of the language it should not deter you from enforcing such restrictions.

To help facilitate these restrictions in my own designs I have created a very simple, yet effective Abstract class called AbstractStaticBase, which helper and utility classes can extend in order to ensure they are never instantiated.

Classes which contain an all static API can extend AbstractStaticBase in order to ensure they can never be instantiated. This is the only requirement.

AbstractStaticBase is lightweight as it only contains a constructor. The constructor does nothing more than create an Error object and parse the call stack to determine the fully qualified name of the concrete class which has been instantiated, the message property is then set on the Error object and thrown.

Implementation on the clients part is very straightforward as all that is required is to extend AbstractStaticBase.

Consider the following example. CalcUtil is an all static class, to ensure an instance of CalcUtil is never instantiated simply extend AbstractStaticBase as follows:

package com.domain.utils
{
    import com.ericfeminella.AbstractStaticBase;

    public class CalcUtil extends AbstractStaticBase
    {
         public static function calculate() : Number
             {
             // implementation not pertinent to subject
         }
    }
}

// an attempt to instantiate CalcUtil …
var util:CalcUtil = new CalcUtil ();
 
// results in the following exception:
// Illegal instantiation attempted
// on class of static type: com.domain.utils::CalcUtil
 

So if you want to enforce that static classes are never instantiated, simply extend AbstractStaticBase.

Multiton Pattern in ActionScript 3

Wednesday, September 26th, 2007

If you are familiar with the standard GoF Patterns than you more than likely are aware of the Singleton Pattern and the solutions which it provides.

For those of you who are not familiar with the Singleton Pattern it is a Creational Pattern which, when implemented as prescribed ensures only one instance of a class is ever instantiated. This is facilitated via a single global access point from which a singleton instance is to be created and or retrieved.

You may be wondering just what the Singleton Pattern has to do with the Multiton Pattern? And how does the Singleton pattern relate to the Multiton pattern? What are the differences and what are the similarities?

To answer your question the Multiton pattern is a Creational pattern which builds on the concept of the Singleton pattern by adding a mapping of key / value [object] pairs.

Unlike the Singleton Pattern, whereas there is only ever a single instance of an object created, the Multiton pattern ensures that only a single instance of an object is created per key. Therefore there are multiple instances which are managed via the Multiton object. The Multiton pattern provides centralized access of Multiton objects and advocates keyed storage of objects within a system.

Below is a simple example which demonstrates an implementation of the Multiton Pattern in ActionScript 3.0:

package
{
  import com.ericfeminella.utils.HashMap;
  import com.ericfeminella.utils.IMap;
   
  public final class Multiton
  {
      private static var instances:IMap = new HashMap();
       
      public function Multiton(access:Private)
      {
          if (access == null)
          {
              throw new Error( "Abstract Exception" );
          }
      }

      public static function getInstance(key:*):Multiton
      {
          var instance:Multiton=instances.getValue(key);

          if ( instance == null )
          {
              instance = new Multiton( new Private() );
              instances.put( key, instance );
          }
          return instance;
      }
       
       public function get id() : *
       {
           return instances.getKey( this );
       }
    }
}

class Private {}

Here is a breakdown of the above example.

First a new class is created as well as an additional inner class outside of the package which is used to ensure the constructor can only be called from within the class body, in this case the Multiton class.

package
{
  public final class Multiton
  {
      public function Multiton(access:Private)
      {
          // verify that access is not null
          // if it is, then an illegal request
          // to instantiate the constructor
          // is being attempted
          if (access == null)
          {
              throw new Error( "Abstract Exception" );
          }
      }
   }
}

/**
 * inner class restricting constructor access to private
 */

class Private {}

Next a private or protected static var of type HashMap (optionally, a generic Object or Dictionary can be substituted) is defined. The static HashMap instance contains the mappings of keys to objects in the Multiton class. Each key only ever contains a single Multiton object instance, and each Multiton instance can only be accessed by it’s associated key.

package
{
  import com.ericfeminella.utils.HashMap;
  import com.ericfeminella.utils.IMap;
   
  public final class Multiton
  {
      // contains key / Multiton instance mappings
      private static var instances:IMap = new HashMap();
       
      public function Multiton(access:Private)
      {
          if (access == null)
          {
              throw new Error( "Abstract Exception" );
          }
      }
   }
}

/**
 * inner class restricting constructor access to private
 */

class Private {}

Lastly, Multiton implementations require a public static method; getInstance(); which is very similar to the static getInstance() as it applies to the Singleton pattern, but with a slightly different signature. The getInstance(); method in a Multiton requires a single parameter which specifies the key from which a new instance is to be assigned and / or retrieved.

Certain Multiton implementations use an object as the key, however it is arguably more intuitive to use a primitive type such as a String to define keys. Regardless, I prefer not to enforce type restraints as the implementation will typically depend on the context in which it is being applied.

package
{
  import com.ericfeminella.utils.HashMap;
  import com.ericfeminella.utils.IMap;
   
  public final class Multiton
  {
      private static var instances:IMap = new HashMap();
       
      public function Multiton(access:Private)
      {
          if (access == null)
          {
              throw new Error( "Abstract Exception" );
          }
      }

      // retrieve the appropriate Multiton instance
      // if the instance does not currently exist
      // one will be instantiated and mapped to
      // the specified key. All subsequent client
      // requests will return the correct instance
      public static function getInstance(key:*):Multiton
      {
          var instance:Multiton=instances.getValue(key);

          if ( instance == null )
          {
              instance = new Multiton( new Private() );
              instances.put( key, instance );
          }
          return instance;
      }
   }
}

class Private {}

To implement a Multiton instance all that is needed is to invoke the static getInstance(); on the Multiton class object just as one would invoke getInstance() on a singleton class object. However in the Multiton it is assumed that there will be many instances, albeit controlled instances, therefore a key must be specified.

Below is a simple example which demonstrates how to retrieve a specific instance of a Multiton object:

var multiton1:Multiton = Multiton.getInstance( "a" );
trace( multiton1.id ); // a

var multiton2:Multiton = Multiton.getInstance( "a" );
trace( multiton2.id ); // a

var multiton3:Multiton = Multiton.getInstance( "o" );
trace( multiton3.id ); // o

There is not to much documentation on the Multiton Pattern outside of the Ruby community and a Java implementation available on wikipedia, however the Multiton Pattern proves very useful when multiple, controlled object instances are needed.

AS3 QueryString API Update

Friday, September 21st, 2007

I have upgraded the QueryString API from an all static utility class to a much more robust API which now supports parsing, inspecting and modifying query strings.

The new QueryString API retrieves a query string for a Flex application via ExternalInterface. Therefore a query string which has been supplied to an application can be retrieved from an html wrapper (e.g. .html, .jsp, .aspx, etc) as well as a .swf file.

Developers can also utilize the new API to perform CRUD operations on an arbitrary query string supplied to the QueryString constructor. I have also added complete support for encoding and decoding a query string as well as appending a query string to separate URLs.

Modifications of a QueryString are now fully supported and allow parameters in a QueryString object to be created, read, updated and deleted.

The QueryString constructor takes a url as an argument. This argument is optional, and, if specified instructs the QueryString object to use the specified url for all subsequent operations. If the url is not specified the QueryString object will assume the aplication query string is to be used for all operations.

Unfortunately, ActionScript 3 does not support constructor / method overloading so the url parameter is used to achieve the correct functionality based on context.

Below is a typical use-case which demonstrates how to instantiate a new QueryString object which defaults to the application’s querystring:

var querystring:IQueryString = new QueryString();
//defaults to application querystring

trace( querystring.getQueryString() );
//outputs application querystring

Optionally, you can choose to use a specific QueryString by passing it to the constructor:

var url:String = "http://localhost/app.html?name=Adobe Flex";

var querystring:IQueryString = new QueryString( url );
//defaults to querystring specified in constructor

trace( querystring.getQueryString() );
//name=Adobe Flex

Additional usage examples for all method can be found in the accompanying ASDoc.

You can view the source for the IQueryString interface and concrete QueryString implementation as well as the asdocs.

QueryString API is protected under the MIT license.