开发者

Use global variables in Flash Builder 4

开发者 https://www.devze.com 2023-01-30 20:18 出处:网络
I would like to use variables throughout my whole Flex project (Flash Builder 4). So I can use them as well as in the main applic开发者_JAVA技巧ation as well as in all the components, services,... Wha

I would like to use variables throughout my whole Flex project (Flash Builder 4). So I can use them as well as in the main applic开发者_JAVA技巧ation as well as in all the components, services,... What is the best way of doing this?

Thanks in advance!


You can use the Singleton Design Pattern to accomplish this. Define your "global" variables with getters/setters.

SingletonExample.getInstance().siteWidth = 550;

There are a lot of ways of writing a singleton class, here is one example:

package
{
    public final class SingletonExample
    {

        private static var _instance : SingletonExample = new SingletonExample();

        public function SingletonExample()
        {
            if(_instance)
                throw new Error( "Singleton and can only be accessed through SingletonExample.getInstance()" ); 
        }

        public static function getInstance() : SingletonExample
        {
            return _instance;
        }

    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消