Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

actionscript 3 - AS3 singleton implementations

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.");
            }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...