Javascript设计模式之--单例模式

    在传统开发工程师眼里,单例就是保证一个类只有一个实例,实现的方法通常是先判断实例存在与否,若是存在直接返回,若是不存在就建立了再返回,这就确保了一个类只有一个实例对象。在JavaScript里,单例做为一个命名空间提供者,从全局命名空间里提供一个惟一的访问点来访问该对象。javascript

    在JavaScript里,实现单例的方式有不少种,其中最简单的一个方式是使用对象字面量的方法,其字面量里能够包含大量的属性和方法: html

var mySingleton = {
    property1: "something",
    property2: "something else",
    method1: function () {
        console.log('hello world');
    }
};

    若是之后要扩展该对象,你能够添加本身的私有成员和方法,而后使用闭包在其内部封装这些变量和函数声明。只暴露你想暴露的public成员和方法,样例代码以下:java

var mySingleton = function () {

    /* 这里声明私有变量和方法 */
    var privateVariable = 'something private';
    function showPrivate() {
        console.log(privateVariable);
    }

    /* 公有变量和方法(能够访问私有变量和方法) */
    return {
        publicMethod: function () {
            showPrivate();
        },
        publicVar: 'the public can see this!'
    };
};

var single = mySingleton();
single.publicMethod();  // 输出 'something private'
console.log(single.publicVar); // 输出 'the public can see this!'

    上面的代码很不错了,但若是咱们想作到只有在使用的时候才初始化,那该如何作呢?为了节约资源的目的,咱们能够另一个构造函数里来初始化这些代码,以下:git

var Singleton = (function () {
    var instantiated;
    function init() {
        /*这里定义单例代码*/
        return {
            publicMethod: function () {
                console.log('hello world');
            },
            publicProperty: 'test'
        };
    }

    return {
        getInstance: function () {
            if (!instantiated) {
                instantiated = init();
            }
            return instantiated;
        }
    };
})();

/*调用公有的方法来获取实例:*/
Singleton.getInstance().publicMethod();

    知道了单例如何实现了,但单例用在什么样的场景比较好呢?其实单例通常是用在系统间各类模式的通讯协调上,下面的代码是一个单例的最佳实践:github

var SingletonTester = (function () {

    //参数:传递给单例的一个参数集合
    function Singleton(args) {

        //设置args变量为接收的参数或者为空(若是没有提供的话)
        var args = args || {};
        //设置name参数
        this.name = 'SingletonTester';
        //设置pointX的值
        this.pointX = args.pointX || 6; //从接收的参数里获取,或者设置为默认值
        //设置pointY的值
        this.pointY = args.pointY || 10;

    }

    //实例容器
    var instance;

    var _static = {
        name: 'SingletonTester',

        //获取实例的方法
        //返回Singleton的实例
        getInstance: function (args) {
            if (instance === undefined) {
                instance = new Singleton(args);
            }
            return instance;
        }
    };
    return _static;
})();

var singletonTest = SingletonTester.getInstance({ pointX: 5 });
console.log(singletonTest.pointX); // 输出 5

其它实现方式


方法1:缓存

function Universe() { // 判断是否存在实例 if (typeof Universe.instance === 'object') { return Universe.instance; } // 其它内容 this.start_time = 0; this.bang = "Big"; // 缓存 Universe.instance = this; // 隐式返回this } // 测试 var uni = new Universe(); var uni2 = new Universe(); console.log(uni === uni2); // true

方法2:闭包

function Universe() { // 缓存的实例 var instance = this; // 其它内容 this.start_time = 0; this.bang = "Big"; // 重写构造函数 Universe = function () { return instance; }; } // 测试 var uni = new Universe(); var uni2 = new Universe(); uni.bang = "123"; console.log(uni === uni2); // true console.log(uni2.bang); // 123

方法3:函数

function Universe() { // 缓存实例 var instance; // 从新构造函数 Universe = function Universe() { return instance; }; // 后期处理原型属性 Universe.prototype = this; // 实例 instance = new Universe(); // 重设构造函数指针 instance.constructor = Universe; // 其它功能 instance.start_time = 0; instance.bang = "Big"; return instance; } // 测试 var uni = new Universe(); var uni2 = new Universe(); console.log(uni === uni2); // true // 添加原型属性 Universe.prototype.nothing = true; var uni = new Universe(); Universe.prototype.everything = true; var uni2 = new Universe(); console.log(uni.nothing); // true console.log(uni2.nothing); // true console.log(uni.everything); // true console.log(uni2.everything); // true console.log(uni.constructor === Universe); // true

方式4:测试

var Universe; (function () { var instance; Universe = function Universe() { if (instance) { return instance; } instance = this; // 其它内容 this.start_time = 0; this.bang = "Big"; }; } ()); //测试代码 var a = new Universe(); var b = new Universe(); alert(a === b); // true a.bang = "123"; alert(b.bang); // 123

参考资料

https://github.com/shichuan/javascript-patterns/blob/master/design-patterns/singleton.htmlthis

http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#singletonpatternjavascript

http://www.cnblogs.com/TomXu/archive/2012/02/20/2352817.html

相关文章
相关标签/搜索