|
Answer» The SINGLETON pattern is a design pattern that is used to restrict instantiation of a class to one object. If we create the class as a singleton then no way to create more than one instance. But, we can GET that single instance in any number of CLASSES. So all the classes will share the same properties and behaviours of that singleton object.
Steps to create a Singleton class:
Consider the MySingleTon class as a singleton class.
package { public class MySingleTon { // Single Instance of Our MySingleTon private static var instance:MySingleTon; //DEFINE YOUR VARIABLES HERE public function MySingleTon (enforcer:SingletonEnforcer) { if (enforcer == null) { throw new Error( "You Can Only Have One MySingleTon"); } } // Returns the Single Instance public static function getInstance() : MySingleTon { if (instance == null) { instance = new MySingleTon ( new SingletonEnforcer ); } return instance;
} } }
// Utility Class to Deny Access to Constructor class SingletonEnforcer {}
- We should create one static variable. It will be called "instance" and it will be of type MySingleTon. This will be the variable where we will store our one instance of our class.
- Then we should create one constructor. The constructor takes one ARGUMENT - "enforcer". You will notice that this "enforcer" has a type of "SingletonEnforcer" which is defined directly after our class. Here is the logic behind that:
- When you put a class in an ActionScript file below the main class, it is only available to that class.
- If the constructor requires this argument – then only our main class can create an instance of itself, because we do not have access to the “SingletonEnforcer” class. Only the main class has this access.
- We will not access our class in the normal way by using the “new” statement because we can’t call the constructor. Once we get inside of the constructor, we have a few lines that MAKE sure things work as planned. The “if” statement ensures that we had a valid “enforcer” passed in. If there wasn’t it throws an Error stating that “You Can Have Only One MySingleTon”.
The singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. If we create the class as a singleton then no way to create more than one instance. But, we can get that single instance in any number of classes. So all the classes will share the same properties and behaviours of that singleton object. Steps to create a Singleton class: Consider the MySingleTon class as a singleton class. package { public class MySingleTon { // Single Instance of Our MySingleTon private static var instance:MySingleTon; //DEFINE YOUR VARIABLES HERE public function MySingleTon (enforcer:SingletonEnforcer) { if (enforcer == null) { throw new Error( "You Can Only Have One MySingleTon"); } } // Returns the Single Instance public static function getInstance() : MySingleTon { if (instance == null) { instance = new MySingleTon ( new SingletonEnforcer ); } return instance; } } } // Utility Class to Deny Access to Constructor class SingletonEnforcer {}
|