Corporate Trainer | Designer | Developer

Corporate Trainer | Designer | Developer

Thursday, January 5, 2012

How to implement Singleton pattern in ActionScript 3.0

Today I will discuss how to implement singleton design pattern using ActionScript 3.0.

Let start with the basics

The Singleton Pattern

This pattern ensures that a class has only one instance. It also ensure that global access is provided to this single instance.

Singletons are generally used in situations where data needs to be kept synchronized or for centralizing

shared resources.

Take for example the situation in which two views display and allow the user to update data, but hold

two separate instances of that data. When one view updates the data, the other view will be out of sync.

However, if both views are pointing to the same object, this problem no longer exists; all you have to do

is refresh the views (which you would probably do by implementing the observer pattern on the model

with the views as observers).

Another possibility is two views that need to access the same external data source via a service call.

If each view has its own instance of the service call, you now have to maintain that code in two different

locations and you will be creating duplicate resources (in the form of objects) that do essentially the same thing. Again, this is not an issue if both views are referencing the same object

A typical singleton class is made up of three major parts. These are:

  • A static variable that holds a reference to the single instance.
  • A public static method (most commonly called getInstance) that provides global access to the stored instance if one already exists and creates it if it does not.
  • Some means of limiting the class to a single instance.

A typical singleton class might look like the following

package com.somedomain.somepackage{

private static instance:SingletonExample;

private function SingletonExample():void{

//any code need to initialize the object

}

public static function getInstance():SingletonExample{

if (SingletonExample.instance == null){

SingletonExample.instance = new SingletonExample();

}

return SingletonExample.instance;

}

}

The single instance of the class is held in the private static instance property. The constructor of the class

is set to private so that it cannot be accessed from outside the class. The public static method getInstance

provides the only method for retrieving the instance, ensuring that all classes that make use of it are

referencing the same instance.

Singleton Pattern implementation in Action Script 3.0

The implementation of a singleton class in ActionScript is slightly different, since ActionScript does not

allow for private constructors. You can simulate a private constructor by using an internal class, as in the

following example:

package{

public class Singleton

{

private static var instance:Singleton;

public function Singleton(enforcer:SingletonEnforcer){}

public static function getInstance():Singleton

{

if (Singleton.instance == null)

{

Singleton.instance = new Singleton(new SingletonEnforcer);

}

return Singleton.instance;

}

}

}

class SingletonEnforcer{}

The class SingletonEnforcer is defined in the same file as the singleton class, but outside the package

definition. When a class is defined in this manner in ActionScript, then only the main class defined in the

file (the one inside the package definition) may access it.

Since the constructor requires an instance of the SingletonEnforcer, any attempt to directly instantiate the

class using the constructor will fail because of an invalid parameter error. However, since the getInstance

function is internal to the class, it may freely access the SingletonEnforcer class and pass it to the

constructor.

Hope this will help you to understand how to implement Singleton pattern in ActionScript 3.0

visit: http://www.masterkingmahendra.com to view my profile and other articles and post.


1 comment: