Singleton Class in Flex

I have doubts ... How would you create a Singleton class in Flex ...

Is there any convention, such as a class name, to be eb Singleton or should it distribute any other class.

How many Singleton elements can a project have?

Can anyone say the use of the Singleton class in real time?

I plan on storing component texts in the Singleton class ... This is a good approach.

+6
flex actionscript
source share
5 answers

Can a worm ask about singles!

There are several different options for creating singleons, mainly because the AS3 does not have private constructors. Here we use the template.

package com.foo.bar { public class Blah { private static var instance : Blah; public function Blah( enforcer : SingletonEnforcer ) {} public static function getInstance() : Blah { if (!instance) { instance = new Blah( new SingletonEnforcer() ); } return instance; } ... } } class SingletonEnforcer{} 

Note that the SingletonEnforcer class is internal, so it can only be used with the Blah class (efficiently). No one can directly instantiate a class, it must go through the getInstance () function.

+17
source share

hope i don't find dead horses here :)

(edit: ahh, I just repeat the phils link)

The Gregors singleton implementation does not protect against calling a constructor with a null value, as in:

 var b:Blah = new Blah(null); 

You will still only have 1 instance, but calling the constructor is still possible with the ensuing consequences.

If you absolutely must use a singleton, the constructor must make sure that the force parameter is not zero.

  public function Blah( enforcer : SingletonEnforcer ) { if(!enforcer){ throw new Error("whoops!"); } } 

You should also be worried about ApplicationDomain when loading swf files. External swf files that use the same definitions can have multiple singleton instances (one in each separate application domain), unless you specify that the swf file must be loaded into an existing applicationdomain.

This means that Blah.getInstance () in AAA.swf is not the same instance as Blah.getinstance () in BBB.swf if AAA.swf loads BBB.swf without an LoaderContext instance that tells the plugin to load BBB.swf in the same ApplicationDomain as AAA.swf

+2
source share

You can first refer to the previous question to find out how to create a singleton class. You can find more information from the presentation of Jacob Fain .

The second question, your project may have technologies, like singleton, in your opinion, but it will create only one instance of each. For example, in cairngorm architecture, you have 3 main singlets: controller, service, and model. The amount of actual class can very much depend on your project.

Finally, there will be real world solutions. You have 2 components that should talk to each other, but you do not want them to know that another exists. The meaning of components is sometimes there, and sometimes they are not ... so you need them to be loosely coupled. You can use singleton to transfer data from one component to another without a direct "conversation" with them.

Using singletones is a good approach if you need to transfer data around your application from component to component and separate them from each other.

0
source share
 package com.foo.bar { public class MySingleton { private static var _instance:MySingleton = new MySingleton; private var _myName:String; public static function get instance():MySingleton { return _instance; } public function set myName(value:String):void { _myName = value; } public function get myName():String { return _myName; } } } 

Note the absence of a constructor here.

0
source share

Hello, you can check out the following Flex Singleton Class example at http://www.how-to-code.com/flex/flex-design-patterns/flex-singleton-class.html

-one
source share

All Articles