【问题标题】:singleton class in FlexFlex 中的单例类
【发布时间】:2010-02-07 15:02:49
【问题描述】:

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

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

谢谢, 拉维

【问题讨论】:

    标签: apache-flex actionscript-3


    【解决方案1】:

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

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

    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;
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-10
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      • 2011-04-07
      • 2019-01-20
      • 1970-01-01
      相关资源
      最近更新 更多