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: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Alex Harui
Sent: Wednesday, March 26, 2008 12:14 AM
To: flexcoders@yahoogroups.com
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: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of dbronk
Sent: Tuesday, March 25, 2008 6:52 PM
To: flexcoders@yahoogroups.com
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 flexcoders@yahoogroups.com, "Alex Harui" <[EMAIL PROTECTED]> wrote:
>
> They would be the same.
> 
> 
> 
> ________________________________
> 
> From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
> Behalf Of lytvynyuk
> Sent: Tuesday, March 25, 2008 12:21 PM
> To: flexcoders@yahoogroups.com
> 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;
> }
>
 


Reply via email to