ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Minimalist Abstract Singleton | 105

Why aren’t there any private classes in ActionScript 3.0? Private
classes haven’t yet been implemented in ECMAScript, and Action-
Script 3.0 has been following the current ECMAScript standards.
Because of a number of non-trivial issues, it turns out that both
ECMAScript and ActionScript 3.0 will have to wait for private classes.
The alternatives were to hold up releasing ActionScript 3.0 or engi-
neer a hack to create a private class constructor.

Consider Example 3-3:


The real key is that the private class is assigned as a parameter in the constructor.


Then, when a new instance is created with thegetInstance( )method, the instance


includes the parameter made up of a new instance of the private class.


Minimalist Abstract Singleton


To illustrate the Singleton design pattern as simply as possible, Figure 3-4 uses the


classic Singleton with very little change. The addedelsestatement shows what hap-


pens when more than a single instance is instantiated at the same time. It will be


removed in the remaining discussion of the Singleton. The othertracestatements in


the script shown in Example 3-4 help to demonstrate how the script runs through


the code.


Example 3-3. Using a private class in a public class constructor


package
{
public class PublicClass
{
private static var instance:PublicClass;
public function PublicClass(pvt:PrivateClass) {
}
public static function getInstance( ):PublicClass
{
PublicClass.instance=new PublicClass (new PrivateClass( ));
}
return PublicClass.instance;
}
}
}
class PrivateClass
{
public function PrivateClass( ) {
trace("Private class is up");
}
}

Free download pdf