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
PureMVC Architects Lounge
Announcements and General Discussion
General Discussion
AS3 implement a better Singleton object
Pages: [
1
]
« previous
next »
Author
Topic: AS3 implement a better Singleton object (Read 12543 times)
nisimjoseph
Full Member
Posts: 22
AS3 implement a better Singleton object
«
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
Re: AS3 implement a better Singleton object
«
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
Re: AS3 implement a better Singleton object
«
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
Re: AS3 implement a better Singleton object
«
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
Re: AS3 implement a better Singleton object
«
Reply #4 on:
September 29, 2008, 07:02:58 »
ok, got it.
Logged
Pages: [
1
]
« previous
next »
Stack Overflow
GitHub
Linked In
Google Plus
Twitter
Powered by SMF 2.0.15
|
SMF © 2006-2007, Simple Machines LLC
|
Content © 2006-2018, Futurescale, Inc.
Loading...