There are some situations where we need to allow only one object to be created for a class. This can be achieved by “Singleton” pattern in as3. We can use the singleton pattern to access a class object globally.
How to prepare a singleton class:-
whenever we want to create a singleton pattern, we should keep two important steps in our mind.
In the following example (SingletonExample.as ), First I have created a singleton class where the constructor has been blocked using throw statement. The throw statement throws an error whenever we tied to create an instance with syntax like var instace:SingletonExample = new SingletonExample();
Example(SingletonExample.as ):-
package
{
public class SingletonExample
{
public function SingletonExample()
{
throw(new Error(“Error: Instantiation failed: You can't instantiate a SingletonExample Class with new keyword”))
}
}
}
var instace:SingletonExample = new SingletonExample();//output: Error: Instantiation failed: You can't instantiate a SingletonExample Class with new keyword
Now we are providing a static property to access SingletonExample class.
Example:-
package
{
private static var _instance:SingletonExample=null
private static var lock:Boolean=true;
public class SingletonExample
{
public static function get instance(): SingletonExample
{
if( _instance==null)
{
lock=false;
_instance=new SingletonExample();
lock=true;
}
return _instance;
}
public function SingletonExample()
{
if(lock)
{throw(new Error(“Error: Instantiation failed: You can't instantiate a SingletonExample Class with new keyword”))}
}
public function message():void
{
trace(“Well Come to Singleton Class”)
}
}// Class closed
} // package closed
By using instance property of the above class we can access the message method as following.
SingletonExample.instance.message()// output panel:- Well Come to Singleton Class
Singleton class vs Static class?
singleton.rar
How to prepare a singleton class:-
whenever we want to create a singleton pattern, we should keep two important steps in our mind.
- We have to block the constructor to restrict instance creations of the class.
- In order to access that class we should provide one static property
In the following example (SingletonExample.as ), First I have created a singleton class where the constructor has been blocked using throw statement. The throw statement throws an error whenever we tied to create an instance with syntax like var instace:SingletonExample = new SingletonExample();
Example(SingletonExample.as ):-
package
{
public class SingletonExample
{
public function SingletonExample()
{
throw(new Error(“Error: Instantiation failed: You can't instantiate a SingletonExample Class with new keyword”))
}
}
}
var instace:SingletonExample = new SingletonExample();//output: Error: Instantiation failed: You can't instantiate a SingletonExample Class with new keyword
Now we are providing a static property to access SingletonExample class.
Example:-
package
{
private static var _instance:SingletonExample=null
private static var lock:Boolean=true;
public class SingletonExample
{
public static function get instance(): SingletonExample
{
if( _instance==null)
{
lock=false;
_instance=new SingletonExample();
lock=true;
}
return _instance;
}
public function SingletonExample()
{
if(lock)
{throw(new Error(“Error: Instantiation failed: You can't instantiate a SingletonExample Class with new keyword”))}
}
public function message():void
{
trace(“Well Come to Singleton Class”)
}
}// Class closed
} // package closed
By using instance property of the above class we can access the message method as following.
SingletonExample.instance.message()// output panel:- Well Come to Singleton Class
Singleton class vs Static class?
- The Singleton class object is created only when we make the first call through instance property. Since it uses lazy instantiation it will save memory consumption. Where as in static classes, all static members will consume more system resources and may slow down the startup process of the application.
- it's easy to re factor . When made the changes from singleton to multiton ('n' of instance) is too easy
- Static methods and properties cannot be inherited.
singleton.rar
Well Done.
ReplyDelete