I am confused: In AS3, why do we keep the Singleton class constructor public
and not private
, like in Java? If we keep the constructor public
, then we can directly access it from the outside!
Please check the MODEL part in this example.
I am confused: In AS3, why do we keep the Singleton class constructor public
and not private
, like in Java? If we keep the constructor public
, then we can directly access it from the outside!
Please check the MODEL part in this example.
Actionscript 3 does not support private constructors.
In order to enforce the singleton pattern, many developers cause the constructor to raise an exception if there is already a singleton instance created. This will cause a runtime error, instead of a compile time error, but it does prevent the singleton's inappropriate use.
Example:
This blog post, in which Sho Kuwamoto (formerly of Macromedia / Adobe and heavily involved in the development of the Flex platform and tooling) explains the decision to omit private and protected constructors from ActionScript 3.0, may be of tangential interest.
Macromedia / Adobe's involvement in the developing ECMAScript 4 standard, and the decision to make ActionScript 3.0 adhere as closely to the specification as possible, meant that they were faced with the choice of either bodging these features, omitting them from the language completely, or waiting until they were standardised (and delaying the release of the language as a result).
Interesting, I think, because it shines a light on some of the key issues in the open versus proprietary standards debate.
Here is an implementation of a singleton in AS with internal class usage:
See details here: http://www.swinburne.edu.au/design/tutorials/P-flash/T-How-to-build-a-Singleton-in-Actionscript-3/ID-143/
With the flex framework comes a class "Singleton" that allows you to
register a class to an interface.
With this method you can hide any functions you want just by not including it in the interface
// Sample.as
// ISample.as
// SampleImpl.as