Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: AS3 implement a better Singleton object  (Read 6042 times)
nisimjoseph
Full Member
***
Posts: 22


View Profile Email
« on: September 28, 2008, 01:04:10 »

the way the Singleton object is implemented in the PureMVC at action script is little hard to work with, why? because it revile only at runtime and not in compile time.
the right way is this:
the class SingletonProtected can be created or even seen only by the Singleton class because it exist only for that class, so no other class in the system can create the Singleton class object.
we get the error at compile time and not at runtime for any try of creation except in this class.


package
{
   public class Singleton
   {
      public function Singleton(sp:SingletonProtected)
      {
      }
      
      public static get instance():Singleton
      {
         if (_instance == null)
         {
            _instance = new Singleton(new SingletonProtected());
         }
         return _instance;
      }
      
      private static var _instance:Singleton = null;

   }
}
class SingletonProtected{}


« Last Edit: September 28, 2008, 01:11:40 by nisimjoseph » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: September 29, 2008, 05:05:48 »

There are a number of ways to implement a Singleton. What do you mean by 'revile at runtime'?
-=Cliff>
Logged
nisimjoseph
Full Member
***
Posts: 22


View Profile Email
« Reply #2 on: September 29, 2008, 05:40:47 »

in the current way it implemented you can see that another instance of the singleton class was created only after you compile and then run the application and then in Runtime you will get error on another creation of this class.
in the above way you will get compilation error on the trying of creation another instance of the singleton.

Nisim
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: September 29, 2008, 06:58:14 »

This method of creating a singleton was ruled out early on because it ends up being more code.

If you write a Singleton like the one above, the enforcer class is only visible to the class whose file you are defining it in.

Remember though, that the framework Singletons are meant to be extended. (In reality only the Facade really does, and you access the others by simply calling their methods on the Facade).

To extend the Facade you would need to add a Singleton enforcer class to your own sublcass, and a constructor that accepts it. In addition to the unavoidable getInstance factory method that returns (or constructs and returns) your concrete facade subclass singleton instance.

Instead, the framework implementation handles ensuring only one instance is created ant throws an error otherwise. You don't get compile time checking, but you have 4 or 5 less boilerplate lines to place in your concrete Facade subclass, and your still guaranteed that only one instance can be created.

-=Cliff> 
Logged
nisimjoseph
Full Member
***
Posts: 22


View Profile Email
« Reply #4 on: September 29, 2008, 07:02:58 »

ok, got it.
Logged
Pages: [1]
Print