Posted By

allnatural on 04/28/09


Tagged

class simple Singleton


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

damarev
spastic
thastyle


Simple Singleton Template


 / Published in: ActionScript 3
 

URL: http://www.destroyyourcomputer.com

Easily create a SINGLE USE ONLY (Singleton) Class Object with this template.

  1. package {
  2. public class Example {
  3. private static var instance:Example;
  4.  
  5. public function Example(access:Access){
  6. if (access != null) {
  7. if (Example.instance == null){
  8. Example.instance = this;
  9. }
  10. } else {
  11. throw new Error(”Illegal operation”);
  12. }
  13. }
  14.  
  15. //provides access to singleton instance
  16. public static function getInstance():Example{
  17. if (instance == null){
  18. instance = new Example();
  19. }
  20. return Example;
  21. }
  22. }
  23. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: joshpope on October 13, 2009

this is incorrect in several areas and would never work as is.

It should be this:

I've renamed example to Functions (as thats how i needed it) package {

public class Functions {

    private static var instance:Functions;

    public function Functions(access:Access){
        if (access != null) {
            if (Functions.instance == null){
                Functions.instance = this;
            }
        } else {
            throw new Error("Illegal operation");
        }
    }

    //provides access to singleton instance
    public static function getInstance():Functions{
        if (instance == null){
            instance = new Functions(new Access);
        }
        return instance;
    }
}

}

class Access {}

You need to login to post a comment.