Flex 中的单例类 [英] singleton class in Flex

查看:34
本文介绍了Flex 中的单例类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Flex 不支持单例类.因为它不访问私有构造函数.

I know singleton class is not supporting in Flex.Because it does not access private constructor.

但我想让一个类是单例类.请任何人都可以举例说明.

But i want to make a class is singleton class. Please anyone can explain with example.

谢谢,拉维

推荐答案

单例是一个只创建一个实例的类.该实例将由程序中的所有其他代码共享.

A singleton is a class of which only one instance will be created. This instance will be shared by all other code in the program.

ActionScript 不支持严格意义上的单例,因为不能将构造函数标记为私有.因此,可以在程序的其他地方创建该类的其他实例.使用以下技巧,您可以确保构造函数仅由单例类本身调用:

A singleton in the strictest sense is not supported in ActionScript because a constructor cannot be marked private. Consequently, additional instances of the class could be created elsewhere in the program. With the following trick, you can ensure that the constructor is only called by the singleton class itself:

package {

public final class Singleton {

    private static var instance:Singleton = new Singleton();

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

    public static function getInstance():Singleton {                        
        return Singleton.instance;
    }
}
}

这篇关于Flex 中的单例类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆