0

I have an AS3 Singleton:

package 
    {
        public class Singleton
        {
            public function Singleton(enforcer:SingletonEnforcer):void
            {
                if(!enforcer){throw new Error("Only one instance of Singleton Class allowed.");}
            }

            private static var _instance:Singleton;
            public static function getInstance():Singleton
            {
                if(!Singleton._instance)
                {
                    Singleton._instance=new Singleton(new SingletonEnforcer());
                }
                return Singleton._instance;
            }
        }
    }
class SingletonEnforcer{}

Consider prop and func() to be a property and method respectively of the Singleton class.

How should I access them?
1. Make them static and use this:

Singleton.getInstance();
Singleton.prop;
Singleton.func();


2. Not make them static and use this:

Singleton.getInstance().prop;
Singleton.getInstance().func();

Does it matter, or is it just visual prefference?

Thank you.

Francisc
  • 77,430
  • 63
  • 180
  • 276

1 Answers1

4

The reason to use a singleton instance is so that you can have class members used in a (relatively) static way.

I won't get into the arguments over whether or not to use a singleton here, there's a very long debate over whether it's a good pattern or not.

Typically, when singletons are used, you store access to them in a local variable and use them like any other class instance. The primary difference, is instead of using:

foo = new Foo();

You use:

foo = Foo.instance;
//Actionscript supports properties which makes this a self-initializing call
-or-
foo = Foo.getInstance();

Followed by

foo.bar();
foo.baz();
foo.fizz = 'buzz';

This doesn't mean that Foo can't have static members of the class, but the rules for adding static members on a Singleton are the same for adding static members to any other class. If the function belongs to the instance, it should be used on the instance, if it belongs to the class, it should be static.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 1
    You can also use `Foo.getInstance().bar()` rather than creating a reference to the singleton. This is probably better if you are only going to use the singleton once in a given class, because it removes the worry of cleaning up additional references at some future point. – shanethehat Aug 19 '11 at 13:57
  • I see, so you are saying, I should use `foo=new Foo();` as I would with any other class, correct? – Francisc Aug 19 '11 at 13:58
  • shanethehat: I use it for DB access. I just want to have `Foo.getInstance().getAllPersons();` etc. (synchronously) – Francisc Aug 19 '11 at 13:59
  • 1
    @shanethehat, I use `Foo.getInstance().bar()` in places where i would use `(new Foo()).bar()` if I were *not* using a singleton. for consistency sake, I prefer to use `foo = Foo.getInstance()` especially in case I want to change to using a different object (or possibly a subclass). If you assign to a local variable, you only have to change one instance of a class name, versus having to change the call *everywehre*. – zzzzBov Aug 19 '11 at 14:20
  • 1
    @zzzzBov - Absolutely, and I do it like you say 9 times of 10. I just wanted to make clear that the alternative does work. – shanethehat Aug 19 '11 at 14:23