110 | Chapter 3: Singleton Pattern
However, does that mean that only a single reference can be associated with the sin-
gle instance? What happens, for example, if I first create one instance with one label,
and then attempt to create another instance with a different label?
To see that a single instance can be associated with more than one reference (name
or label), some changes have to be made to the primary Singleton class. Using a sepa-
rate folder from theSingletonclass, in Example 3-4 open a new ActionScript file and
add the script in Example 3-8. Save it asSingleton.as in the newly created folder.
Example 3-8. Singleton.as
package
{
public class Singleton
{
private var _msg:String;
private static var _instance:Singleton;
public function Singleton(pvt:PrivateClass)
{
}
public static function getInstance( ):Singleton
{
if(Singleton._instance == null)
{
Singleton._instance=new Singleton(new PrivateClass( ));
trace("Singleton instantiated");
}
return Singleton._instance;
}
//Get and set methods
public function getMsg( ):String
{
return _msg;
}
public function setMsg(alert:String):void
{
_msg = alert;
}
}
}
class PrivateClass
{
public function PrivateClass( ) {
trace("PrivateClass called");
}
}