You are currently browsing archived articles from

AS3 Singleton Pattern Implementation

Saturday, September 16th, 2006

Many times when developing you will find that your application calls for a single instance of a class to manage a specific service across the system. For example, let’s say that your application needs to have one single instance of a class that manages all events within the system. Or another scenario could be where the application calls for a single instance of a class to manage all WSDL calls across the system. In order to support the required functionality for the application you would need to create a class that implements what is known as the Singleton design pattern.

The Singleton pattern is implemented by creating a class that has a static public method which instantiates a new instance of the object if one does not exist. If an instance already exists, it simply returns a reference to that object which is a static property of the class, typically named ‘instance’. A best practice naming convention for this method is ‘getInstance();’. To make sure that the object cannot be instantiated in any other way, the constructor is made either private or protected. A Singleton is also considered to be an anti-pattern to some extent as it can be used as a euphemism for a global variable. However, in Object Oriented Programming it is an extreamly useful tool when used correctly.

As I mentioned earlier, the constructor for a Singleton is made either private or protected to restrict it’s access from being instantiated. Since constructors can only be declared as public in ActionScript 3 this is not allowed. A common solution to this is to simply add a inner class within the same file as the class definition and pass an instance of the private class as an argument to the constructor. This way only the getInstance(); method has access to the private class. The only problem with this approach is that the constructor can be called from anywhere within the application with a null value passed in as the argument to the constructor. My solution to this is simple, check that the parameter passed to the constructor is not null. This is how a true Singleton implementation is achieved. The only time that this will not work is if the singleton class is a sub class and the constructor needs to make a call to super. The reason that this will not work is that a call to a super classes constructor must be executed in the very first line of code, therefore a parameter value can not be validated.

Below I have added a simple example demonstrating how to implement the Singleton pattern in ActionScript 3.0:

AS3 Custom Tween API

Monday, September 4th, 2006

A few months ago I was experimenting with the new Tween classes available in ActionScript 3.0. After importing the Tween class from the new package I received the following error when I attempting to instantiate a Tween instance
ReferenceError: Error #1069: Property onTweenEnd not found on mx.controls.DataGrid

I found that this error would occur no matter what type of component I attempted to call a Tween on. This error I assume was being called on or during an interval call as it was firing constantly.

So after spending a decent amount of time going back and forth trying different things I simply decided it would be easier to write my own Tween API for AS3. My intentions were to simply provide a basic Tween API for an application that I was developing at work. all I wanted to do was have the ability to easily apply Tweens to a component in Flex 2. I wanted the tween to be simular to that which was available in ActionScript 2 under the mx.transitions packages.

The custom Tween API is easy to use and also very useful so I have decided to distribute it as an .swc file for other Flex Developers to use in their Flex 2.0 Projects.

The Tween API consists of 3 destinct classes:

  1. Tween – Public class which provides all Tween functionality
  2. TweenType – Static class which provides a simple set of public constants for providing Tween types
  3. TweenEffect – Internal Static class which provides all Tweening methods which are from Robert Penner’s Tweening equations

Usage is pretty straight forward. All you have to do is import the Tween and TweenType classes and instantiate an instance of the Tween class. The Tween class constructor excepts 6 parameters which are as follows:

  1. tweenTarget:DisplayObjectContainer,
  2. propertyToTween:String,
  3. tweenType:String,
  4. tweenStartPosition:Number,
  5. tweenStopPosition:Number,
  6. tweenDuration:Number

The second parameter, propertyToTween can be set by using a constant from the TweenType class. This class is useful and enables you to easily see what type of tweens are available via the list of public constants.

Future revisions will include a callback event handler which will fire once the Tween is completed. I will continue to post updates once they are completed.

You can download the Tween API (version 0.1.9) for AS3 by clicking here.

AS3 JSONDecoding Class

Saturday, August 26th, 2006

I added a simple JSON decoding class to the ActionScript 3 source code page of my blog today. This is a useful base class which you can extend to use a JSON object as a data source.

The JSONDecoder class defines a simple set of methods for loading a JSON encoded object from a specific URI, decoding the result into an ArrayCollection of ActionScript Objects, and displaying any errors that may occur.

JSON (JavaScript Object Notation) objects are a lightweight data-interchange format which are very useful due to the fact that they are language independent. JSON objects are simple objects comprised of name / value pairs which are easy to use with any language such as C, C++, C#, Java, JavaScript, ActionScript etc.

AS3 DeepCopy Utility Class

Thursday, August 17th, 2006

In ActionScript 3 when you create an object and set it’s value to that of an existing object, both the new object and the original object point to the same reference. This is known as creating a “shallow copy” of the original object. In a shallow copy, only a reference to the original object is created rather than creating a copy of the object directly.

For instance if you were to do the folowing:

var objA:Object = {id: “Object A: Original ID”};
var objB:Object = {id: “Object B: Original ID”};
var arrA:Array = new Array(objA, objB);
var arrB:Array = arrA; = “Some new value”;
“Some new value”
“Some new value”

As you can see in the example this can potentially cause problems if you need the indices in the array to not reflect changes in the objects in which they reference. In order to resolve this we would need to make what is known as a ‘deep copy’ of the objects in the Array. In a deep copy the objects that are being referenced are copied to a new memory location rather then pointing to the location of the reference objects themselves.

I wrote a simple class that uses a ByteArray to create a deep copy of the original objects. So if you wanted to create copies of the original Objects you could do so as in the following:

var objA:Object = {id: “Object A: Original ID”};
var objB:Object = {id: “Object B: Original ID”};
var arrA:Array = new Array(objA, objB);
var arrB:Array = DeepCopy.clone(arrA); = “Some new value”;
“Some new value”
“Object A: Original ID”

Below I have provided a link to view the source code.


Real-World Flex Seminar in NYC

Tuesday, August 1st, 2006

I will be attending the “Real World Flex Seminar” aptly titled “Next Generation Application Development with Flex” in NYC on August 14th located at the Roosevelt Hotel.

Speakers will include some of the industries leading Flex developers including Chris Coenraets and Jesse Warden as well as many others who are helping to push Flex into the next generation of RIA development.

For more information check out :

The Future of ActionScript (ECMA 4)

Friday, July 28th, 2006

The ECMA has released resources to the public reflecting the 4th edition of the standard. Being that ActionScript implements the ECMAScript standard, these future implementations will most likely become part of ActionScript in the future.

Adobe has members on the committee which is evident as many of the new features are following ActionScript 3’s lead (such as strict and standard modes).

The link below is a good starting point to see where ActionScript, not to mention Java, C# and many other languages will be heading in the future.