"A class containing a static variable that stores a unique, and inaccesible
to external classes (private), intance of itself. The static variable is
accessed by a static method, with public access, usually called getIntance.
The static variable is initiated by the static getInstance method that
validates wether or not the static variable already exits. If the static
variable has not being initiated, a new instance of the class is created
and assigned to the static variable which reference is then returned by the
method. If the static variable was previously created, the method will
return a reference to the static variable."
1
A Singleton class is used when you wish to
restrict instantiation of a
class to only one
object. "Simple Singleton Pattern Example in AS3
class Data
{
private static var dataInstance:Data;
public static function getInstance():Data
{
if(!dataInstance) dataInstance = new Data();
return dataInstance;
}
public function Data()
{
if(dataInstance) throw Error("instance exists, please use Data.getInstance()");
}
}
"
21 [Daniel Guzman - AS3 Object Oriented Programming]
2 [Daniel Guzman - AS3 Object Oriented Programming]