zwetan wrote:
Have anyone got ideas how to implement Singleton pattern in AS3 best?
Constructor in AS3 can only be public or internal, which is not private
anyway. ..


Well, internal should be enough to implement a Singleton


Just have a public const in the package initialized with an internal class
of the package


Something like that

myTest/MySingleton.as
-----------------------------
package myTest
  {
  public const MySingleton:Singleton = new Singleton();
  }
-----------------------------

myTest/Singleton.as
-----------------------------
Package myTest
  {

  //default to internal, can only be accessed inside the package
  class Singleton
     {

     //be sure to let the constructor internal
     function Singleton()
        {
        //...
        }

     //be sure to have your methods/properties/etc. public
     //so it can be accessed outside of the package
     public function someMethod():String
        {
        //...
        }
     }
  }
-----------------------------

And after well

import myTest.MySingleton;

MySingleton.someMethod()
etc.


I don't see the need of a private constructors at all

And furthermore private constructors imho would cause
more problems than solutions

What people oversee is that we are in ECMAScript,
A Singleton in this context should just be an object
That you can not copy and/or inherit in another prototype
So a public read-only object.

Here having an internal class inside a package prevent
to instanciate this class outside of the package
the public const force to have only one global access point to the
instancied object
So where is the problem ?

Not having private constructors is not a bug,
It's using a private constructor to implement singleton
which is imho a bug/hack from AS2 times.

http://www.mozilla.org/js/language/es4/core/definitions.html

private - Makes the definition visible only in the enclosing class's private
namespace

so following the standard if you got a private constructor
you just CAN NOT instanciate at all the class anywhere else
than inside the class !

private constructors just does not make sens.

zwetan

They do. With internal access modifier you're still able to create an instance of class directly.

--
Michael "Antares" Klishin,

Email: [EMAIL PROTECTED]
Web: www.novemberain.com

Non progredi est regredi
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to