| Jon Rose’s Blog | | Enterprise Software Consultant Open Source / Java / Flex Flex Practice Director @ Gorilla Logic, Inc. InfoQ.com RIA Editor |
![]() |
Comments:
Gerald Yeo on May 21st, 2007 at 1:57 am #
The work around would be this: package com.flashobscura{ public function SingletonDemo() { } public static function getInstance():SingletonDemo {
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: Implementation: For details visit:http://www.ericfeminella.com/blog/2007/01/16/pseudo-abstract-classes-in-as3/ Singleton Example: 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
|
|