Thanks for the link, I'll check it out (but I'm from a Java world so I like
my .getInstance() ;-) )

Here's my singleton test implementation:



package com.juicability.moduletesting
{
 public class TestSingleton
 {
  private static var instance:TestSingleton;  // the only instance of this
singleton

  /**
   * Singleton - use getInstance() instead!
   **/
  public function TestSingleton()
  {
   if (TestSingleton.instance != null)
   {
    throw new Error("TestSingleton is a singleton class, getInstance()
should be used.");
   }
  }

  /**
   * Get the only instance of the TestSingleton
   * @return TestSingleton instance
   **/
  public static function getInstance():TestSingleton
  {
   if (instance == null)
   {
    instance = new TestSingleton();
    trace("TestSingleton - instance was null, so created new instance");
   } else {
    trace("TestSingleton already instanciated, so returning instance..");
   }
   return instance;
  }
 }
}




On 05/12/2007, riaengineer <[EMAIL PROTECTED]> wrote:
>
>   I've found using Darron Schall's singleton implementation seems to
> work great for my projects.
>
> http://www.darronschall.com/weblog/archives/000274.cfm
>
> Short of that can you post your singleton implementation code (or at
> least the constructor) ?
>
> --- In flexcoders@yahoogroups.com <flexcoders%40yahoogroups.com>, "bjorn
> -" <[EMAIL PROTECTED]> wrote:
> >
> > I've made a simple testcase with a singleton and three Flex projects:
> >
> > 1. MainApp
> > 2. ModuleOne
> > 3. ModuleTwo
> >
> > MainApp loads ModuleOne which loads ModuleTwo (it could also load both
> > modules, the behaviour I describe below seems to be the same in both
> cases).
> >
> > I have created a TestSingleton.as which implements the Singleton
> pattern (as
> > far as it can be tanken in AS anyway ..)
> >
> > Here's TestSingleton.as's getInstance() method:
> >
> > /**
> > * Get the only instance of the TestSingleton
> > * @return TestSingleton instance
> > **/
> > public static function getInstance():TestSingleton
> > {
> > if (instance == null)
> > {
> > instance = new TestSingleton();
> > trace("TestSingleton - instance was null, so created new instance");
> > } else {
> > trace("TestSingleton already instanciated, so returning
> instance..");
> > }
> > return instance;
> > }
> >
> >
> > If I put the singleton in MainApp, and then getInstance() it from
> ModuleOne
> > and ModuleTwo I'll get this in my trace:
> >
> >
> > [SWF] C:\dev\FlexWorkspace\ModuleTestMainApp\bin\ModuleTestModuleOne-
> > debug.swf - 450,372 bytes after decompression
> > TestSingleton - instance was null, so created new instance
> > [SWF] C:\dev\FlexWorkspace\ModuleTestMainApp\bin\ModuleTestModuleTwo-
> > debug.swf - 446,193 bytes after decompression
> > TestSingleton already instanciated, so returning instance..
> >
> > .. which is the expected behaviour.
> >
> > However, if I put TestSingleton.as in ModuleTwo, I get this:
> >
> > [SWF] C:\dev\FlexWorkspace\ModuleTestMainApp\bin\ModuleTestModuleOne-
> > debug.swf - 450,372 bytes after decompression
> > TestSingleton - instance was null, so created new instance
> > [SWF] C:\dev\FlexWorkspace\ModuleTestMainApp\bin\ModuleTestModuleTwo-
> > debug.swf - 446,193 bytes after decompression
> > TestSingleton - instance was null, so created new instance
> >
> > .. it doesn't behave as a singleton anymore ..
> >
> > Can anyone explain why? :-)
> >
> > --
> >
> > ========================
> > http://www.juicability.com - flex blog
> > http://www.43min.com - funny movies
> >
>
> 
>



-- 

========================
http://www.juicability.com - flex blog
http://www.43min.com - funny movies

Reply via email to