Archives

While I don’t recommend it as standard practice, there have been a few cases where I have found it useful to use a set of static (class level) methods to proxy a singleton implementation. This simplifies access so that you can do this:

MyClass.myMethod(param);

MyClass.myMethod(param);

instead of this:

MyClass.getInstance().myMethod(param);

MyClass.getInstance().myMethod(param);

This can be achieved relatively simply, while maintaining the advantages of Singleton, by exposing static methods that pass requests on to instance methods via a standard getInstance call:

Where this gets a little tricky is if you want the class to act as an event dispatcher. The class needs to expose addEventListener and removeEventListener as static methods, but if you use EventDispatcher.initialize(this) in the constructor, it will try to set up those methods on the instance. This will cause problems, because in Flash, you can’t have class and instance level methods with the same name. Instead, you can set up a generic object to act as an event dispatcher, and redirect all the event logic to it.

Because it should look like the events are being sent from the class, not the generic object, you’ll also want to intercept dispatchEvent calls and set the target. The result looks something like this:

As much as possible, I am trying to preserve the Singleton implementation whilst wrapping it in a simpler interface.

Injecting things into a class using Mixins seems really messy to me (and iirc it doesn’t work properly with EventDispatcher for some reason anyway).

An important part of Singleton is the abstraction of instantiation. I suppose being that this is all within a single class, I could reference the instance directly, but I prefer to have that extra layer and know I could change the instantiation rules by editing a single method.

EventDispatcher doesnât work when you try to initialize it from a static method because the class is not available yet. You canât reference ANY CLASS you import from inside of a static method if that method is being called from the class itself (unless there is a way to specify load order ala AS1 #initclip ORDER)

Your example works because you are calling getInstance on a method call which is being made from the timeline after all classes have loaded / initialized. However, if you changed the static member instance to call getInstance() directly you would lose reference to any class you are importing.

So while I like how your solution works around the import issue I still prefer not to try to hack a static class because of the added bloat from having to have 1 static method for every instance method. In addition, you lose the benefit of being able to reference the class using a variable and declaring its type (IE: var a:StaticInterfaceDemo = StaticInterfaceDemo; a.myMethod(4);) Granted, you could use getInstance if you made your getInstance method public, but then the code would be a little inconsistent and could just add confusion.

I agree, this method adds a little bit to file size, and has a small amount of overhead associated with redirecting the method calls. This is why I said I don’t recommend it as a standard practice, but it can be handy at times.

In the few cases where I’ve found this to be useful, it’s for low access classes with slim APIs, so the overhead is minimal, and being able to reference it as a typed instance is a non-issue.

I was recently introduced to by the book âobject oriented action script for flash 8 â to a great way to use the â__resolveâ function in a decorator implementation, I was wondering if there is a way to use â__resolveâ function to get a direct access to singleton function as well without the need to declare a new object?