zoukankan      html  css  js  c++  java
  • 单例模式i

    单例模式:保证一个类仅有一个实例,并且提供一个全局访问

    使用场景:比如点击按钮出现弹框,这个弹框是唯一的,无论点击多少次,这个弹框只会被创建一次。

    实现方式:用一个变量来标志当前时候已经为某个类创建过对象,如果是,则在下一次获取该类的实例的时候直接返回之前创建的对象。

    var SingleClass=function(name){
        this.name=name;
        //标志当前是否已经创建过对象
        this.instance=null;
    }
    
    SingleClass.getInstance=function(name){
       if (!this.instance) {
           return this.instance=new SingleClass(name);
       }else{
            return this.instance;
       }
    }

    也可以写成

    var SingleClass = function(name) {
        this.name = name;
    }
    
    
    SingleClass.getInstance = (function() {
            var instance = null;
            return function(name) {
                if (!instance) {
                    return instance = new SingleClass(name);
                } else {
                    return instance;
                }
            }
    })();

    透明化单例模式

    var CreateDiv = (function() {
        var instance = null;
        var CreateDiv = function(html) {
            if (!instance) {
                this.html = html;
                this.init();
                instance = this; //this是实例化的这个对象
            } else {
                return instance;
            }
        }
        CreateDiv.prototype.init = function() {
            var div = document.createElement('div');
            div.innerHTML = this.html;
            document.body.appendChild(div);
        }
    
        return CreateDiv;
    })();
    var a = new CreateDiv('a');
    var b = new CreateDiv('b');
    alert(a===b)  //true

    代理类:把负责管理单例的逻辑移到了来李磊,这样CreateDiv就是普通的类

    //代理类
    var CreateDiv = function(html) {
        this.html = html;
        this.init();
    };
    CreateDiv.prototype.init = function() {
        var div = document.createElement('div');
        div.innerHTML = this.html;
        document.body.appendChild(div);
    }
    var ProxicySingleCreateDiv = (function() {
        var instance = null;
        return function(html) {
    
            if (!instance) {
                instance = new CreateDiv(html);
    
            }
            return instance;
        }
    })();
    
    var a = new ProxicySingleCreateDiv('a');
    var b = new ProxicySingleCreateDiv('b');

    传统的单例模式使用了类,但是JavaScript不需要。

    惰性单例:在需要的时候才创建对象的实例,就像之前的调用SingleClass.getInstance的时候才被创建

    SingleClass.getInstance = (function() {
        var instance = null;
        return function(name) {
            if (!instance) {
                return instance = new SingleClass(name);
            } else {
                return instance;
            }
        }
    })();

    不过这是基于类的单例模式,在JavaScript中不适用

  • 相关阅读:
    pytest之断言
    python之self
    python标准数据结构类型
    pytest之fixture
    python之继承和多态
    安卓UI自动化,pytest+UIautomator2+allure+jenkins
    airtest
    Python中单下划线开头的特性
    系统默认分配的共享内存太小,导致zabbix_server无法启动
    运行yum报错Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
  • 原文地址:https://www.cnblogs.com/t1amo/p/6772884.html
Copyright © 2011-2022 走看看