AS3单实施 [英] AS3 singleton implementations

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

问题描述

我看到单身了这么多实现左右,而我只是想一个单身的

在第一次通话1:实例 2:实例只有一次(杜)

因此​​,在性能和最低的内存单耗,什么这样做的最好的实现?

例1

 包单身
{
    公共类SomeClass的
    {
        私人静止无功_instance:SomeClass的;
        公共职能AlertIcons(E:拦截器):无效{}
        公共静态函数获得实例():SomeClass的{
            测试!= NULL || (测试=新SomeClass的(新的拦截器()));
            返回_instance;
        }
    }
}
一流的拦截器{}
 

例2

 公共final类辛格尔顿
    {
        私人静止无功_instance:单身=新辛格尔顿();

        公共职能辛格尔顿()
        {
            如果(_instance!= NULL)
            {
                抛出新的错误(辛格尔顿只能通过Singleton.instance访问);
            }
        }

        公共静态函数获得实例():辛格尔顿
        {
            返回_instance;
        }
    }
 

例3

 包{
公共类SingletonDemo {
私人静止无功实例:SingletonDemo;
私人静止无功allowInstantiation:布尔;
公共静态函数的getInstance():SingletonDemo {
如果(例如== NULL){
allowInstantiation =真;
例如=新SingletonDemo();
allowInstantiation = FALSE;
}
返回实例;
}
公共职能SingletonDemo():无效{
如果(!allowInstantiation){
抛出新的错误(错误:实例化失败:使用SingletonDemo.getInstance(),而不是新的。);
}
}
}
}
 

解决方案

例如2,但与一捻,因为你应该允许新辛格尔顿()被调用至少一次,我不喜欢实例化的东西,直到我需要他们,从而例如在第一次调用()实际创建实例......后续调用抢原来的。

编辑:如何扬着它也可以让你要求

  VAR单:单身=新辛格尔顿();
 

它会工作......但未来所有的尝试都将抛出错误和力量使用getInstance()方法的

 公共final类辛格尔顿{
    私人静止无功_instance:辛格尔顿;

    公共职能辛格尔顿(){
        如果(_instance){
            抛出新的错误(单身......使用的getInstance());
        }
        _instance =这一点;
    }

    公共静态函数的getInstance():辛格尔顿{
        如果(!_实例){
            新辛格尔顿();
        }
        返回_instance;
    }
}
 

I have seen so many implementations of singletons around, and i just want a singleton that

1.- instances on the first call 2.- instances only once (duh)

So in performance and lowest memory consumtion, whats the best implementation for this?

Example 1

package Singletons
{
    public class someClass
    {
        private static var _instance:someClass;
        public function AlertIcons(e:Blocker):void{}
        public static function get instance():someClass{
            test!=null || (test=new someClass(new Blocker()));
            return _instance;
        }
    }
}
class Blocker{}

Example2

public final class Singleton
    {
        private static var _instance:Singleton = new Singleton();

        public function Singleton()
        {
            if (_instance != null)
            {
                throw new Error("Singleton can only be accessed through Singleton.instance");
            }
        }

        public static function get instance():Singleton
        {
            return _instance;
        }
    }

Example 3

package {
public class SingletonDemo {
private static var instance:SingletonDemo;
private static var allowInstantiation:Boolean;
public static function getInstance():SingletonDemo {
if (instance == null) {
allowInstantiation = true;
instance = new SingletonDemo();
allowInstantiation = false;
}
return instance;
}
public function SingletonDemo():void {
if (!allowInstantiation) {
throw new Error("Error: Instantiation failed: Use SingletonDemo.getInstance() instead of new.");
}
}
}
}

解决方案

example 2 but with a twist since you should allow new Singleton() to be called at least once and I don't like instantiating things until I need them, thus the first call to instance() actually creates the instance... subsequent calls grab the original.

EDIT: Sowed how it can also allow for if you call

var singleton:Singleton = new Singleton();

it will work... but all future tries will throw the error and force use of the getInstance() method

public final class Singleton{
    private static var _instance:Singleton;

    public function Singleton(){
        if(_instance){
            throw new Error("Singleton... use getInstance()");
        } 
        _instance = this;
    }

    public static function getInstance():Singleton{
        if(!_instance){
            new Singleton();
        } 
        return _instance;
    }
}

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

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