Following on from my rant yesterday …
Christophe Herreman, author of the Prana framework, posted a very neat solution to the problem of AS3’s lack of private constructors when trying to create a typesafe enum class, back in November of last year. (I noticed a similar post on BarneyBlog from a couple days earlier, too.)
Basically you use a static code block to set a variable - this is done at compile time - i.e., after the constants are defined, but before any calls to the constructor. Depending on the value of this variable, the constructor can then throw an exception - effectively disallowing any further class instances to be created. This would allow singletons to be created also.
This does not, however, reflect well on Adobe, but rather on the dilligence of the developer community. I can find no reference to static code blocks in the official documentation either for Flex or for Flash. The fact we have to rely on undocumented features to implement (very basic) industry standard design patterns is a shameful state of affairs.


Recent Comments