第四章、单例模式

第四章、单例模式

单例模式的核心:唯一的实例,在全局能访问到

全局变量不是单例模式,但会把全局变量当作单例模式使用。

减少全局模式的使用方法:
1.使用命名空间:

let A = {
   
	add(){
   }
}
A.add()

2.使用闭包封装私有变量

把一些变量封装到闭包内部,只暴露一些接口

const user = (function() {
   
	var name = 'a',
        age = 29
    return {
   
		getUserInfo : function() {
   
			return name+age;
        }
    }
})()

4.1惰性单例

在需要的时候才去创对象实例

应用场景:弹窗、购物车等

let timeTool = (function() {
   
   let _instance = null;
   function init() {
   
       //私有变量
       let now  = new Date();
       //共有属性方法
       let name = '时间处理工具';
       this.getTime = function() {
   
           return now.toTimeString();
       }
   }
    return function() {
   
        if(!_instance) {
   
            _instance = new init();
        }
        return _instance;
    }
 })();
let instance1 = timeTool();
let instance2 = timeTool();
console.log(instance1 === instance2); //true

上面的timeTool实际上是一个函数,_instance作为实例对象最开始赋值为nullinit函数是其构造函数,用于实例化对象,立即执行函数返回的是匿名函数用于判断实例是否创建,只有当调用timeTool()时进行实例的实例化,这就是惰性单例的应用,不在js加载时就进行实例化创建, 而是在需要的时候再进行单例的创建。 如果再次调用, 那么返回的永远是第一次实例化后的实例对象。

4.2扩展 ES6中创建单例模式

import export就是单例模式

使用ES6的语法将constructor改写为单例模式的构造器。

class SingletonApple {
   
  constructor(name, creator, products) {
   
    //首次使用构造器实例
    if (!SingletonApple.instance) {
   
      this.name = name;
      this.creator = creator;
      this.products = products;
      //将this挂载到SingletonApple这个类的instance属性上
      SingletonApple.instance = this;
    }
    return SingletonApple.instance;
  }
}
/*
  constructor(name, creator, products) {
      this.name = name;
      this.creator = creator;
      this.products = products;
  }
  //静态方法
  static getInstance(name, creator, products) {
    if(!this.instance) {
      this.instance = new SingletonApple(name, creator, products);
    }
    return this.instance;
  }
}
*/

let appleCompany = new SingletonApple('苹果公司', '乔布斯', ['iPhone', 'iMac', 'iPad', 'iPod']);
let copyApple = new SingletonApple('苹果公司', '阿辉', ['iPhone', 'iMac', 'iPad', 'iPod']);

console.log(appleCompany === copyApple);  //true

单例模式案例——登录框 (codepen.io)

相关推荐

  1. 模式

    2024-02-23 09:42:04       61 阅读
  2. 《C++新经典设计模式》之7 模式

    2024-02-23 09:42:04       45 阅读
  3. 模式模板

    2024-02-23 09:42:04       43 阅读
  4. 模式种具体写法

    2024-02-23 09:42:04       59 阅读
  5. 模式【C#】

    2024-02-23 09:42:04       54 阅读
  6. python模式

    2024-02-23 09:42:04       63 阅读
  7. 模式详解

    2024-02-23 09:42:04       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-23 09:42:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-23 09:42:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-23 09:42:04       87 阅读
  4. Python语言-面向对象

    2024-02-23 09:42:04       96 阅读

热门阅读

  1. Kotlin 中注解 @JvmOverloads 的作用

    2024-02-23 09:42:04       53 阅读
  2. Spring Boot 常用注解大全

    2024-02-23 09:42:04       48 阅读
  3. ECMAScript modules规范示例详解

    2024-02-23 09:42:04       51 阅读
  4. Selenium基础:自动化你的网页交互

    2024-02-23 09:42:04       58 阅读
  5. Spring Boot使用MongoDB详解

    2024-02-23 09:42:04       51 阅读
  6. Apache Camel定时任务

    2024-02-23 09:42:04       50 阅读
  7. 数据仓库和数据湖的区别

    2024-02-23 09:42:04       47 阅读
  8. Stable Diffusion算法、结构全流程概述

    2024-02-23 09:42:04       48 阅读
  9. spark ui的job数,stage数以及task数

    2024-02-23 09:42:04       45 阅读
  10. 云原生 - K8s命令合集

    2024-02-23 09:42:04       52 阅读