Make that *public* static function init (root : DisplayObject)

http://nondocs.blogspot.com/2007/04/metadatamixin.html

-Josh

On Thu, Jul 17, 2008 at 8:44 AM, Josh McDonald <[EMAIL PROTECTED]> wrote:

> **Rant (and code) follows**
>
> Ew, I hate that pattern. Well I'm not fond of client-aware singleton-dom
> either, but ew. What the hell does that "outside the package" definition do?
> Add a package-protected class to the default package? Add stuff to Global?
> Disappear it into fat air? What about name conflicts?
>
> If you're stuck with non-injection, try to use an interface and put
> "getInstance" into a public  static helper / locator function in a public
> "Locator" class, that instantiates a package-protected class with public
> methods.
>
> Since we can't hide the constructor, all "enforce singleton" patterns are
> going to be ugly in AS3. I know two files are a pain, but try and keep the
> voodoo inside your package and out of the global namespace :)
>
> If you want "new SingletonClass()" to fail at compile-time like the above
> example, create a proper class with a custom namespace to do the enforcing
> instead of the throwaway default package class. Call it
> "DONTUSEME::ThisIsASingleton" if you wanna make things clear :) You can also
> enforce it at runtime if you like, using something like the below code.
>
> Oh, and make it "function get instance()" instead of "function
> getInstance()", this is ActionScript.
>
> If you're stuck doing a one-file singleton because you're too used to Java
> *ducks*, do it something like this:
>
> [Mixin]
> public final class SingletonClass {
>
>   static private var __enforceSingletonKey : Number;
>   static private var __instance : SingletonClass;
>
>   static function init (root : DisplayObject) : void
>   {
>     //It's just safer to do it here than on the static declaration above.
> And you'll dodge some compiler bugs that misreport errors :)
>     __enforceSingletonKey = Math.random();
>   }
>
>   function SingletonClass(key : Number)
>   {
>     if (key != __enforceSingletonKey) throw new Error("Don't instantiate me
> bro!");
>   }
>
>   public static function get instance() : SingletonClass
>   {
>     if (!__instance)
>       __instance = new SingletonClass(__enforceSingletonKey);
>
>     return instance;
>   }
> }
>
> -Josh
>
>
> On Thu, Jul 17, 2008 at 4:55 AM, dbronk <[EMAIL PROTECTED]> wrote:
>
>> Here is my preferred way of creating a singleton class.  Usage would
>> be: MySingleton.instance.someVar
>>
>>
>> Dale
>>
>> --------------------------------------------------------------------
>>
>> package your.package
>> {
>>
>> [Bindable]
>> final public class MySingleton
>> {
>> public static const instance : MySingleton= new
>> MySingleton(SingletonLock);
>>
>> public var someVar : String = "Hello";
>>
>> public function MySingleton(lockClass:Class)
>> {
>> if ( lockClass != SingletonLock || instance != null )
>> {
>> throw new Error("Invalid instantiation.  Please use instance.");
>> }
>> }
>>
>> }
>> }
>>
>> class SingletonLock {}
>>
>> --------------------------------------------------------------------
>>
>>
>>
>>
>>
>> --- In flexcoders@yahoogroups.com, "Tracy Spratt" <[EMAIL PROTECTED]> wrote:
>> >
>> > A singleton class will have a static getInstance() method.
>> >
>> >
>> >
>> > Anywhere you need to access members of the singleton, call that method.
>> > Just import the class, do not use the normal instantiation procedures,
>> > like new, or using an mxml tag.
>> >
>> > import mypackage.MyModel;
>> >
>> > [Bindable]private var _myModel:MyModel; //optional var reference
>> >
>> >
>> >
>> > Private function init(): void
>> >
>> >   _myModel = MyModel.getInstance();
>> >
>> >   Alert.show(_myModel.myFunction())
>> >
>> >
>> >
>> > <mx:Text ...text="{_myModel.myBindablePublicProperty}"
>> >
>> >
>> >
>> > Tracy
>> >
>> >
>> >
>> > ________________________________
>> >
>> > From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
>> > Behalf Of Scott
>> > Sent: Wednesday, July 16, 2008 12:42 PM
>> > To: flexcoders@yahoogroups.com
>> > Subject: [flexcoders] Anyone have a good resource on singletons?
>> >
>> >
>> >
>> > I'm trying to find information on using singletons in AS3/Flex.  I've
>> > got an .AS file set up but I'm having issues calling the data/functions
>> > within that function in other classes.  Does anyone have a good resource
>> > on the web for creating and using singletons?
>> >
>> >
>> >
>> > Thanks!
>> >
>>
>>
>>
>> ------------------------------------
>>
>> --
>> Flexcoders Mailing List
>> FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
>> Search Archives:
>> http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups
>> Links
>>
>>
>>
>>
>
>
> --
> "Therefore, send not to know For whom the bell tolls. It tolls for thee."
>
> :: Josh 'G-Funk' McDonald
> :: 0437 221 380 :: [EMAIL PROTECTED]
>



-- 
"Therefore, send not to know For whom the bell tolls. It tolls for thee."

:: Josh 'G-Funk' McDonald
:: 0437 221 380 :: [EMAIL PROTECTED]

Reply via email to