I'm in crunch mode right now, but in a week or two when it slows a bit
I'll try and recreate what I had. How did I make sure that the
instances were different? By going into debug mode and checking the
memory address of each as well as setting a variable in the singleton
from the application and the module not seeing that change. How did I
create my singleton?
public class Congif
{
public static const instance : Congif = new Congif(SingletonLock);
public function Congif(clazz:Class)
{
if ( clazz != SingletonLock )
{
throw new Error("Invalid instantiation. Use instance.");
}
}
}
class SingletonLock {}
Is the problem that I use a static const instance and have it set
itself rather than a getInstance() function? I like doing it this way
as it takes less code. This is much closer to how I would create a
singleton in Java. I wish that AS3 would just simply allow a private
constructor and then we would do away with all these different
"singleton-ish" implementations. Also, _if/when_ we ever get
threading, my implementation will not break, but the getInstance with
checking if instance == null will. Of course, threading is a long,
long way away, if ever so I know that is kind of a moot point right now.
If you see an issue with how I create my singletons let me know.
Otherwise I'll place a reminder for myself to try and rebuild what I
had in a couple weeks.
Dale
--- In [email protected], "Rick Winscot" <[EMAIL PROTECTED]>
wrote:
>
> Alex the effect he is describing is due to the lack of a
protected/locally
> visible SingletonEnforcer passed to the constructor of the singleton and
> proper management/return of the local static class instance. A
pattern for
> the ActionScript singleton should be...
>
> public class MySingletonClass
> {
>
> private static var _mySingletonClass:MySingletonClass;
>
> public function MySingletonClass( enforcer:SingletonEnforcer ) { }
>
> public static function getInstance():MySingletonClass
> {
> if ( _mySingletonClass == null )
> _mySingletonClass = new MySingletonClass( new SingletonEnforcer );
>
> return _mySingletonClass;
> }
>
> }
>
> class SingletonEnforcer{}
>
> If this pattern is used - the MySingletonClass.getInstance() and var
> foo:MySingletonClass = new MySingletonClass() will resolve to the same
> object... although they may appear (in name) as two separate
objects. You
> can verify this by launching into debug mode and looking at the @093240
> (memory address) of both objects. If you've done it right - they
will be the
> same.
>
>
> Rick Winscot
>
>
>
> From: [email protected] [mailto:[EMAIL PROTECTED] On
> Behalf Of Alex Harui
> Sent: Wednesday, March 26, 2008 12:14 AM
> To: [email protected]
> Subject: RE: [flexcoders] Re: Singletone class in Application and
Modules !?
>
> You'll have to send me a test case. They should be the same.
>
> ________________________________________
> From: [email protected] [mailto:[EMAIL PROTECTED] On
> Behalf Of dbronk
> Sent: Tuesday, March 25, 2008 6:52 PM
> To: [email protected]
> Subject: [flexcoders] Re: Singletone class in Application and Modules !?
>
> I beg to differ. I had setup my app thinking this to be true. When I
> did this the module would get it's own, and very different, instance
> of my singleton. I had to change the way I did things so what I do
> now is upon loading my module, the main app then sets the singleton
> into the module. I don't have my example code anymore, but it was
> quite easy to produce the results.
>
> Maybe it has to do with the way I compiled my module???? I compiled
> my application with "-link-report=c:\app\encoreNG.link.report.xml" and
> my module with "-load-externs=c:\app\encoreNG.link.report.xml". Not
> sure if that has anything to do with it. Also, when I had this coded
> I was on Flex 3, Beta 3 and have not retested with Flex 3 GA.
>
> Dale
>
> --- In [email protected], "Alex Harui" <aharui@> wrote:
> >
> > They would be the same.
> >
> >
> >
> > ________________________________
> >
> > From: [email protected]
[mailto:[EMAIL PROTECTED] On
> > Behalf Of lytvynyuk
> > Sent: Tuesday, March 25, 2008 12:21 PM
> > To: [email protected]
> > Subject: [flexcoders] Singletone class in Application and Modules !?
> >
> >
> >
> > Is it true if I have singleton class instantiated in main application
> > and in module, instances will be different?
> >
> > public class DataManager
> > {
> > private static const singleton_:DataManager = new DataManager();
> >
> > public static function get instance():DataManager {
> > return singleton_;
> > }
> >
> > public function DataManager() {
> > if (singleton_ != null) {
> > throw new Error("Cannot instantiate singleton
> > DataManager");
> > ! }
> > }
> > }
> >
> > in main application:
> >
> > private function init():void {
> > this.dataManager = DataManager.instance;
> > }
> >
> > in module
> >
> > private function init():void {
> > this.dataManager = DataManager.instance;
> > }
> >
>