Singleton Pattern in AS3

Singleton Pattern in AS3AS3 does not support private or protected constructors which makes it harder to implement the singleton pattern. Below are some approaches I’ve run across on the Internet, problems with them, and what I hope please tell me if I’m wrong corrections to get a real singleton pattern working.

via Daniel R.: Singleton Pattern in AS3.

Die sauberste Lösung

package
{
    public class Singleton
    {
	private static var _instance:Singleton;
 
	public function Singleton(lock:SingletonLock)
	{
	}
 
	public static function getInstance():Singleton
	{
            if (_instance == null)
                _instance = new Singleton(new SingletonLock());
 
            return _instance;
	}
    }
}
 
class SingletonLock
{
}

http://en.wikipedia.org/wiki/Singleton_pattern#Flash_ActionScript

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>