Singleton Design Pattern Actionscript 3

When Classes, Constant variables or functions  are needed throughout the actionscript project freuquently i tend to wrap them into a new, separate Actionscrip class …

Within this class i will make an instance of that class , which will only be made once … as a result of this all the declarations will be held in 1 place … but accessable from every corner of the project …

More info about this design pattern: here

How does this pattern look in actionscript?

Singleton Design Pattern

Singleton Design Pattern

Point 1 :  define a instance of the class inside the actionscript class as a statis variable.

Point 2:  make a function getInstance() , this will fetch and return the instance variable , if this instance is still empty when it is called, it will set the instance to a new instance of the class, but only ONCE.

Access the instance from uitside the class (example , don’t mind the imageLoader part)

Accessing The Instance

Accessing The Instance

Using this way of working you can save important and frequent functions, variables … in 1 place … very organised: nice & clean, isn’t that what it is all about  … !

1 comment so far

  1. Ibrahim Ersoy on

    Man :) thanks 100 times :D

    u saved me a couple of days

    see u later ;)


Leave a reply