Singleton Class in Flex

2019-04-28 06:58发布

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

Is there any convention like the class name should eb Singleton or it should extend any other class.

How many Singleton class can a project have?

Can anyone say the real time usage of a Singleton class?

I am planning to keep my components label texts in a Singleton class... Is it a good approach.

5条回答
相关推荐>>
2楼-- · 2019-04-28 07:46
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;
        }

    }
}

Notice the absence of a constructor here.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-04-28 07:49

First you can reference a previous question to find out how to create a singleton class. You can find more info from a Yakov Fain presentation as well.

Second question, your project can technology have as may singleton class as you see fit but it will only create 1 instance of each. For example, in the cairngorm architecture you have 3 main singletons: controller, service and model. The number of actual class can very depending on your project.

Finally, A real world solutions would be. You have 2 components that need to talk to each other but you don't want them to know the other exists. Meaning sometimes the components are there and sometimes they are not...so you need them to be loosely coupled. you can uses singletons to pass the data from one component to the other with out "talking" to them directly.

Using singletons is a good approach if you need to pass data around your application from component to component and would like to decouple them from each other.

查看更多
做自己的国王
4楼-- · 2019-04-28 07:52

Can of worms asking about singletons!

There are a few different options about creating singletons mainly due to AS3 not having private constructors. Here's the pattern we use.

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 can only be used by the Blah class (effectively). No-one can directly instantiate the class, they have to go through the getInstance() function.

查看更多
Evening l夕情丶
5楼-- · 2019-04-28 07:55

hope I'm not hitting dead horses here :)

(edit: ahh, I'm just repeating phils link)

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

var b:Blah = new Blah(null);

You will still have only 1 instance, but invoking the constructor is still possible with the consequences that follows.

If you absolutely must enforce the singleton, the constructor should make sure that the enforcer parameter isn't null.

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

You should also be concerned about ApplicationDomain when loading swf files. External swf files that uses the same definitions, may have multiple singleton instances (1 in each separate applicationdomain) if you do not specify that the swf file must be loaded into the 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 a LoaderContext instance that tells the plugin to load BBB.swf into the same ApplicationDomain as AAA.swf

查看更多
ゆ 、 Hurt°
6楼-- · 2019-04-28 07:57

Hello you could check out the following of a Flex Singleton Class example on http://www.how-to-code.com/flex/flex-design-patterns/flex-singleton-class.html

查看更多
登录 后发表回答