Filed Under (ActionScript, Flex) by jonr on May-21-2007

I am a bit surprised to find out that ActionScript 3 doesn’t allow me to declare my constructor as private or protected. So much for implementing a true Singleton.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • LinkedIn

Comments:
Gerald Yeo on May 21st, 2007 at 1:57 am #

The work around would be this:

package com.flashobscura{
public class SingletonDemo {
private static var INSTANCE:SingletonDemo = null;
private static var ALLOWINSTANTIATION:Boolean = false;

public function SingletonDemo() {
if (getQualifiedClassName(super) == “com.flashobscura::SingletonDemo” ) {
if (!ALLOWINSTANTIATION) {
throw new Error(“Error: Instantiation failed: Use SingletonDemo.getInstance() instead of new.”);
}
else {
// do initialisation
}
}

}

public static function getInstance():SingletonDemo {
if (INSTANCE == null) {
ALLOWINSTANTIATION = true;
INSTANCE = new SingletonDemo();
ALLOWINSTANTIATION = false;
}
return INSTANCE;
}
}
}

jonr on May 21st, 2007 at 2:14 am #

Thanks for the comment Gerald!

The workaround you provided is sufficient. Although, I do still find it annoying that I cannot enforce the access limitation at compile time, and can only throw an error during runtime.

Coming from a Java development background, this limitation of ActionScript 3 surprised me. Although, I do understand it is a limitation to make ActionScript 3 compliant with the ECMA Script specification.

Keith Peters on May 21st, 2007 at 3:07 am #

Yes, this has been talked about for the last year and a half, extensively. There are a few workarounds. The one given above is that it only throws a runtime error. There’s another technique using instance of a helper class that is passed into the constructor. Since the helper class is only available to the file itself, you will get a compile error if you try to instantiate it using the constructor. I’m sure you can dig up several examples of this with a quick search. The problem with that one though, is that you can still get around it by passing in null to the constructor.

Eric Feminella on May 21st, 2007 at 8:41 pm #

It is possible to create pseudo-abstract classes as well as true Singletons in ActionScript 3 via inner classes. Below is how I would typically implement a pseudo-abstract class as well as a true Singleton:

Abstract Example:
package {
public class Abstract {
public function Abstract(access:Private) {
if(access == null){
throw new Error(“NullPointerException…”);
}
}
protected static final function getAccess():Private{
return new Private();
}
}
}
class Private{}

Implementation:
package {
public class SubClass extends Abstract {
public function SubClass(){
super( getAccess() );
}
}
}

For details visit:http://www.ericfeminella.com/blog/2007/01/16/pseudo-abstract-classes-in-as3/

Singleton Example:
package {
public class SingletonExample {
private static var instance:SingletonExample;
public function SingletonExample (access:Private) {
if(access == null){
throw new Error(“NullPointerException…”);
}
instance = this;
}
public static function getInstance() : SingletonExample {
if (instance == null)
{
instance = new SingletonExample( new Private );
}
return instance;
}
}

Hope this helps.

- Eric

class Private {}

Ciprian on November 24th, 2009 at 10:10 am #

I’m looking (for a while) to build a true singleton, one that is not instantiable at compile time. These work-arrounds that throw errors may be fine if you really don’t have a choice but I don’t like them. I will keep searching for the solution until I find it.

Post a comment
Name: 
Email: 
URL: 
Comments: