当前位置: 首页 > news >正文

单例模式,适用于对象唯一的情景(设计模式与开发实践 P4)

文章目录

    • 单例模式
    • 实现
    • 代理单例
    • 惰性单例

上一章后续的内容是关于 JS 函数闭包的,考虑很多读者已经有了闭包基础或者希望通过实战理解,遂跳过上一章直接开始设计模式篇~

需要注意的是,代码部分仅供参考,主要关注的内容是设计模式的思想和运用,结合到自己的实战中才是最好的

单例模式

单例模式的定义是:保证一个类只有一个实例,并提供一个访问他的全局访问点

例如:线程池,全局缓存,登陆窗口(无论你点击多少次,窗口都只会创建一个)

实现

实现起来并不复杂,只需要创建一个变量来标识是否已经为某个类创建过对象,如果已经创建了,那就直接返回之前创建的对象~

const Singleton = {instance: null,getInstance: function () {if (!this.instance) {this.instance = {// 在这里定义单例的属性和方法name: "Singleton Instance",sayHello: function () {console.log("Hello from Singleton!");},};}return this.instance;},
};// 使用示例
const instance1 = Singleton.getInstance();
console.log(instance1.name); // 输出: Singleton Instance
instance1.sayHello(); // 输出: Hello from Singleton!const instance2 = Singleton.getInstance();
console.log(instance2.name); // 输出: Singleton Instanceconsole.log(instance1 === instance2); // 输出: true

方便结合理解,我们加入一个静态型面向对象的语言 C# 来看:

public class Singleton
{private static Singleton instance;// 私有构造函数,防止外部实例化private Singleton(){}public static Singleton Instance{get{if (instance == null){instance = new Singleton();}return instance;}}// 在这里定义单例的其他属性和方法public string Name { get; set; }public void SayHello(){Console.WriteLine("Hello from Singleton!");}
}

代理单例

上面的代码有一个问题,你会发现 JS 部分中,一部分代码用来保证单例不重复创建,另一部分代码用来创建单例对象,显然这不是一个好的做法,如果某一天我们要改写这个单例代码,无疑会使代码变得复杂,所以引入 代理 单例模式

通过这样就使得 Singleton 成为了一个普通的类,和 Proxy 组成了单例模式!

var Singleton = function () {this.name = "Singleton Instance";
};Singleton.prototype.sayHello = function () {console.log("Hello from Singleton!");
};const SingletonProxy = (function () {var instance;return function () {if (!instance) {instance = new Singleton();}return instance;};
})();// 使用示例
var proxyInstance1 = new SingletonProxy();
var proxyInstance2 = new SingletonProxy();console.log(proxyInstance1.name);
console.log(proxyInstance2.name);

同样还有 C# 版的:

private class Singleton
{private Singleton(){// 构造函数}
}public class SingletonProxy
{private Singleton proxyInstance;public Singleton GetInstance(){if (proxyInstance == null){proxyInstance = Singleton.Instance;}return proxyInstance;}
}

惰性单例

上面的代码已经实现了惰性单例模式:只有调用的时候才实现对象的初始化,即这一段:

public Singleton GetInstance()
{if (proxyInstance == null){proxyInstance = Singleton.Instance;}return proxyInstance;
}

这样使得资源和配置更灵活,且线程安全~

http://www.lryc.cn/news/167438.html

相关文章:

  • C语言实现三子棋游戏(详解)
  • javaee之黑马乐优商城3
  • Pytorch intermediate(二) ResNet
  • 【2023集创赛】加速科技杯作品:高光响应的二硫化铼光电探测器
  • 编写postcss插件,全局css文件px转vw
  • 精品SpringCloud的B2C模式在线学习网微服务分布式
  • 解决vue项目导出当前页Table为Excel
  • C++设计模式_04_Strategy 策略模式
  • 目标检测YOLO实战应用案例100讲-基于YOLOv3多模块融合的遥感目标检测(中)
  • element 表格fixed列高度无法100%
  • 【接口自动化测试】Eolink Apilkit 安装部署,支持 Windows、Mac、Linux 等系统
  • 解决sass问题:npm ERR! node-sass@9.0.0 postinstall: `node scripts/build.js`
  • Python技巧---tqdm库的使用
  • linux-线程条件变量(cond)
  • 面试算法6:排序数组中的两个数字之和
  • 【智能家居-大模型】构建未来,聆思大模型智能家居交互解决方案正式发布
  • 通讯网关软件002——利用CommGate X2HTTP-U实现HTTP访问OPC UA Server
  • 模拟经营类游戏是怎么开发的?
  • 基于JAVA+SSM+微信小程序+MySql的图书捐赠管理系统设计与实现
  • 软件设计模式系列之六——单例模式
  • verdi dump状态机的波形时直接显示状态名
  • 代码随想录算法训练营19期第53天
  • 二刷力扣--栈和队列
  • 第六章 图 十、关键路径
  • Virtualbox固定存储硬盘转换为动态存储硬盘
  • 【栈与队列面试题】有效的括号(动图演示)
  • 基于matlab实现的弹簧振动系统模型程序(动态模型)
  • 哨兵1号(Sentinel-1)SAR卫星介绍
  • [maven] scopes 管理 profile 测试覆盖率
  • css网页打印字体设置