Not to pick on Jeff (because even our docs use the same approach), but
I'm not a fan of this solution, because its trivial to pass in "null"
where its expecting a "PrivateClass".  To protect against that, you need
to have a runtime check, and throw.  Yuck.

My personal solution is as follows (warning: non-syntactic)

// IManager.as
package {
public interface IManager {...}
}

// Manager.as
package {
public class Manager
{
  private static var _impl:ManagerImpl = null;
  public static function getInstance():IManager
  {
    if (_impl == null) _impl = new ManagerImpl();
    return _impl;
  }
}
}
// outside the package decl!
class ManagerImpl implements IManager {..}

Benefits: no way to make a ManagerImpl from outside, because it has no
visibility.  Compile-time checking.  Implementation decoupling.

One objection: "but they could make a new Manager!".  My first response
is "so what?", because the key thing for a singleton is usually to
guarantee a correctly running application by ensuring that an invariant
(only one impl in existence) is not violated.  By this metric, they can
create "Manager" all day, and its harmless, because its just the
wrapper.  However, my second response is that if this bothers you, you
could also just make a "getManager()" call.  I personally find
"getManager.as" to be jarring, so I prefer a static method, but YMMV.

-rg
 

> -----Original Message-----
> From: [email protected] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Jeff Tapper
> Sent: Monday, March 20, 2006 7:06 AM
> To: [email protected]
> Subject: Re: [flexcoders] Singleton not usable?
> 
> We've solved this one a few times.  I've blogged about it in my AS3 
> Datamanager entry.  take a look.
> 
> http://jeff.mxdj.com/as3_datamanager.htm
> 
> At 09:24 AM 3/20/2006, you wrote:
> >Hi all,
> >
> >I'm trying to make a singleton class as demonstrated below, 
> but get the 
> >error: "a constructor may only be declared 'public'". Well, 
> that error is 
> >clear, the reason for it is not however.
> >Isn't it possible anymore to make a singleton class, or is 
> there another 
> >method for it?
> >
> >Class Singleton {
> >
> >private static var _instance:Singleton = null;
> >
> >private function Sinleton() {
> >}
> >
> >public static function getInstance():Singleton {
> >    if(Singleton._instance == null) {
> >       _instance = new Singleton();
> >    }
> >    return _instance;
> >}
> >
> >}
> >
> >Any help would be much appreciated.
> >
> >Kind regards,
> >Sonja Duijvesteijn
> >
> >
> >--
> >Flexcoders Mailing List
> >FAQ: 
> ><http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ
> .txt>http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
> >Search Archives: 
> ><http://www.mail-archive.com/flexcoders%40yahoogroups.com>htt
p://www.mail-archive.com/flexcoders%40yahoogroups.com 
> >
> >
> >
> >
> >----------
> >YAHOO! GROUPS LINKS
> >
> >    *  Visit your group 
> > "<http://groups.yahoo.com/group/flexcoders>flexcoders" on the web.
> >    *
> >    *  To unsubscribe from this group, send an email to:
> >    * 
> > 
> <mailto:[EMAIL PROTECTED]
ribe>[EMAIL PROTECTED] 
> >
> >    *
> >    *  Your use of Yahoo! Groups is subject to the 
> > <http://docs.yahoo.com/info/terms/>Yahoo! Terms of Service.
> >
> >
> >----------
> 
> 
> 
> 
> --
> Flexcoders Mailing List
> FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
> Search Archives: 
> http://www.mail-archive.com/flexcoders%40yahoogroups.com 
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 


--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to