JS acquaintance design pattern (a) - Singleton

First, the definition

Defined design patterns are: object-oriented software design process is simple and elegant solutions to specific problems, simple language that design pattern is a solution to a problem in certain situations. If another popular thing to say, some good design patterns is to give a name designed object-oriented software development. That design pattern is not difficult, but it is currently only achieved some of the best recognized resolve certain issues.

Example 1. Single Mode

Singleton pattern is defined: to ensure only one instance of a class, and it provides access to a global access point. For example: the thread pool, global cache, the browser window and other objects, which require only one object instance is sufficient.

Example single-mode 1.1

To achieve Singleton pattern is not complicated, just use a variable to indicate whether to create a class instance too, and if so, get the next instance of the class when the direct return has been cached good example, or create and caches this instance.

1. achieve a singleton pattern


    // 单例模式实现一
    function SingleTon(name) {
        this.name = name;
        this.instance = null;
    }
    SingleTon.prototype.getName = function() {
        return this.name;
    }
    SingleTon.getInstance = function(name) {
        if (this.instance) {
            return this.instance;
        }
        return new SingleTon(name);
    }
复制代码
  1. Two singleton pattern implemented by closure
    // 单例模式实现二, 利用闭包
    function SingleTon2(name) {
        this.name = name;
    }
    SingleTon2.getInstance = (function(){
        let instance = null;
        return function(name) {
            if (!instance) {
                instance = new SingleTon2(name);
            }
            return instance;
        }
    })();
复制代码
  1. Three implemented by class
    // 实现三 类
    class SingleTon3 {
        constructor(props) {
            const { name, ...other } = props;
            this.name = name;
            this.instance = null; // 标识是否已经创建过实例
        }
        setName = () => {
            return this.name;
        }
        static getInstance = (name) => {
            this.instance = this.instance ? this.instance : new SingleTon3(name); 
            return this.instance;
        }
    }

    const t1 = SingleTon3.getInstance('timo1');
    const t2 = SingleTon3.getInstance('timo2');
    console.log('t1 => ',t1); // timo1
    console.log('t2 => ',t2); // timo1
    console.log('t1 === t2 => ',t1 === t2); // true
复制代码

Note: class (class) is defined by the static keyword static methods. You can not call a static method on an instance of the class, but rather by calling the class itself. These methods are generally utilities, such as creating functions or clone

4. derived, using closure to encapsulate private variables

    // 使用闭包来封装私有变量
    const user = (function() {
        let name = 'timo';
        let age = 22;
        return {
            getUserInfo: function() {
                return name + ' '+ age;
            }
        };
    })();
复制代码

Special attention: let const statement and does not bind to the global window object! ! ! The use var statement is automatically bound to the window

to sum up:

Singleton pattern is a simple but very useful model, in particular an inert art single embodiment, only create the object at the right time, and creates only a single.

Reproduced in: https: //juejin.im/post/5cfa1f2be51d45773d4685da

Guess you like

Origin blog.csdn.net/weixin_33670786/article/details/91475783