Subcribe via RSS

Singleton Pattern in ActionScript 3

May 15th, 2011 | Posted in ActionScript3

As AS3 does not support the use of private constructors, we have to use a workaround in order to implement the Singleton pattern.

We can find lots of different approaches on the Internet as for example the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  package  {
 
  public class SingletonClass {

    private static var _instance:SingletonClass;

    public function SingletonClass(enforcer:SingletonEnforcer) {
      if (enforcer == null) {
        throw new Error("SingletonClass is a Singleton class and must be accessed using getInstance()");
      }
    }
   
    public static function getInstance():SingletonClass {
      if (_instance == null) {
        _instance = new SingletonClass(new SingletonEnforcer());
      }
      return _instance;
    }
   
    public function testFunction():void {
      trace("test function");
    }
  }
}

class SingletonEnforcer {}

We use an intenal class called SingletonEnforcer in the constructor. The class is defined outside the package and it will be accessible only from the classes inside the .as file. This way we prevent the instantiation of our Singleton class without the use of the method getInstance().

The next approach is much more neat and looks like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package  {
 
  public class SingletonClass {
   
    private static var _instance:SingletonClass = new SingletonClass();

    public function SingletonClass() {
      if (_instance) throw new Error("SingletonClass is a Singleton class and must be accessed using getInstance()");
    }
   
    public static function getInstance():SingletonClass {
      return _instance;
    }
   
    public function testFunction():void {
      trace("test function");
    }
  }
}

This implementation looks much better but creates an instance automatically, and sometimes we would prefer to have the class in memory only when we need it.

The last approach I think it’s the best from my point of view:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package  {
 
  public class SingletonClass {

    private static var _instance:SingletonClass;
    private static var _allowInstance:Boolean;

    public function SingletonClass() {
      if (!_allowInstance) throw new Error("SingletonClass is a Singleton class and must be accessed using getInstance()");
    }
   
    public static function getInstance():SingletonClass {
      if (_instance == null) {
        _allowInstance = true;
        _instance = new SingletonClass();
        _allowInstance = false;
      }
      return _instance;
    }
   
    public function testFunction():void {
      trace("test function");
    }
  }
}

As I said before, there are multiple ways of implementing this design pattern. In an ideal world we will be able to use private constructors in ActionScript, but meanwhile this happens we can use our customized Singleton classes in AS3!


Cheers.

Leave a Reply