The Singleton: One Class Instance for All Others to Use
The Singleton is a way of structuring your class that will allow you to reference the same instance of it from multiple other classes throughout your project. Knowing how to use the singleton class correctly will allow you code more freely and efficiently, make cleaner and more readable classes, and also improve the garbage collection/performance of your game.
So what is a singleton class, exactly
Well, you create a singleton class just like you would any other class. In flash builder you can right-click your project (or need package in which you want to class to appear) and choose new – action script class. This will create a class stub that you should already be pretty familiar. Now normally in the constructor function we would just say in it to get the class started enrolling. But for a singleton class it's going to be a little different. Basically, we are going to have a function public function that returns an instance of the class instead of creating an instance with the new keyword.
Show me the code
Were going to introduce two new field properties that we will need for the singleton class. One is a Boolean variable allowInstantiation. As soon as the class is created the first time that's going in change to false. The other is a variable called instance, and did this as a data type that is the same as the current classes name. I know this may seem a bit weird at first but this class actually creates a new instance of itself and then saves that reference in to this variable.
The get instance method
As I mentioned before,when another class wants to Credo reference of creating an instance of the Singleton it does not use the new keyword. Instead, we call a public static function on the singleton class that returns the instance. Usually this function is named get instance because it is used to gain instance of the class!
So what is a singleton class, exactly
Well, you create a singleton class just like you would any other class. In flash builder you can right-click your project (or need package in which you want to class to appear) and choose new – action script class. This will create a class stub that you should already be pretty familiar. Now normally in the constructor function we would just say in it to get the class started enrolling. But for a singleton class it's going to be a little different. Basically, we are going to have a function public function that returns an instance of the class instead of creating an instance with the new keyword.
Show me the code
Were going to introduce two new field properties that we will need for the singleton class. One is a Boolean variable allowInstantiation. As soon as the class is created the first time that's going in change to false. The other is a variable called instance, and did this as a data type that is the same as the current classes name. I know this may seem a bit weird at first but this class actually creates a new instance of itself and then saves that reference in to this variable.
The get instance method
As I mentioned before,when another class wants to Credo reference of creating an instance of the Singleton it does not use the new keyword. Instead, we call a public static function on the singleton class that returns the instance. Usually this function is named get instance because it is used to gain instance of the class!