Actionscript
範例來源
----------------------------------------------------------------------------------------------------
package
{
public class Singleton
{
public static var instance:Singleton;
public static function getInstance():Singleton
{
if( instance == null ) instance = new Singleton( new SingletonEnforcer() );
return instance;
}
public function Singleton( pvt:SingletonEnforcer )
{
// init class
}
}
}
internal class SingletonEnforcer{}
-------------------------------------------------------------------------------------------------{
public class Singleton
{
public static var instance:Singleton;
public static function getInstance():Singleton
{
if( instance == null ) instance = new Singleton( new SingletonEnforcer() );
return instance;
}
public function Singleton( pvt:SingletonEnforcer )
{
// init class
}
}
}
internal class SingletonEnforcer{}
之前看到actionscipt可以在一個as檔裡面同時塞很多個類別的時候,
心中的第一個反應就是,為什麼會允許使用這種感覺不是很好的寫作風格呢?
沒想到可以用這個特性建立Singleton