Created
December 26, 2009 06:09
-
-
Save rjungemann/263855 to your computer and use it in GitHub Desktop.
An example of the Singleton pattern in ActionScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This code comes from http://www.darronschall.com/weblog/2007/11/actionscript-3-singleton-redux.cfm | |
*/ | |
// in SingletonExample.as | |
package com.thefifthcircuit.util { | |
public class SingletonExample { | |
public var foo:String; | |
private static const inst:SingletonExample = new SingletonExample(SingletonLock); | |
public static function get instance():SingletonExample { return inst; } | |
public function SingletonExample(lock:Class) { | |
if(lock != SingletonLock) throw new Error("Invalid Singleton access. Use LoaderSingleton.instance."); | |
foo = "bar"; | |
} | |
} | |
} | |
// in SingletonLock.as | |
package com.thefifthcircuit.util { | |
public class SingletonLock {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment