个人博客:github.com/ruizhengyun…javascript
在回答这个问题以前,先说下什么是单例模式。 单例模式又叫单体模式,保证一个类仅有一个实例,这意味着第二次使用同一个类建立新对象时,获得的是与第一次所建立的对象彻底相同,并提供全局访问点。java
抓住关键词 “惟一” 和 “全局访问” 的对象,不经让我想起全局对象。git
// 全局对象
var globaObj = {};
复制代码
but,使用全局变量会有如下问题:es6
全局变量问题折中的应对方案:github
1.java 实现设计模式
// 0.0.3/Singleton.java
public class Singleton {
// 私有化构造函数,即外部不能使用 new Singleton(),外部不能使用new!!
private Singleton(){}
// 内部 new
private Singleton instance = null;
// 对外接口
public Singleton getInstance() {
if(instance === null) {
// 保证只会 new 一次
instance = new Singleton();
}
return instance;
}
//对象方法
public void show(name, pwd) {
System.out.printIn('展现');
}
}
public class SingletonDemo {
public static void main(String[] args) {
// 不合法
Singleton object = new Singleton();
// 正确使用,惟一可用可用对象
Singleton object = Singleton.getInstance();
object.show();
}
}
复制代码
2.javascript 简单实现 使用一个变量存储类实例对象(值初始为 null/undefined
)。进行类实例化时,判断类实例对象是否存在,存在则返回该实例,不存在则建立类实例后返回。屡次调用类生成实例方法,返回同一个实例对象。闭包
// 0.0.3/Singleton.js
class Singleton {
constructor(name) {
this.name = name;
this.instance = null;
}
show() {
console.log(this.name);
}
}
Singleton.getInstance = function (name) {
if (this.instance) {
return this.instance;
}
return this.instance = new Singleton(name);
}
// 实例
// 只能使用静态函数 getInstance,不能使用 new Singleton(),可是只能文档约束
let s1 = Singleton.getInstance('展现1');
s1.show();
let s2 = Singleton.getInstance('展现2');
s2.show();
console.log(s1 === s2); // true
let s3 = new Singleton('展现3');
s3.show();
let s4 = new Singleton('展现4');
s4.show();
console.log(s3 === s4); // false
复制代码
上面 s1 === s2
为 true
,而 s3 === s4
为 false
,缘由在于 s1
和 s2
在堆内存中指向同一地址, 而 s3
和 s4
在堆内存开辟了两套空间。ide
存在问题函数
Singleton.getInstance(...)
;3.javascript 透明实现 统一用 new
操做符获取单例,而不是使用 Singleton.getInstance(...)
post
// 0.0.3/Singleton2.js
let Singleton = (function () {
let instance
return function (name) {
if (!instance) {
this.name = name;
return instance = this;
}
return instance;
}
})();
Singleton.prototype.show = function () {
console.log(this.name);
}
// 实例
let s3 = new Singleton('展现3');
s3.show();
let s4 = new Singleton('展现4');
s4.show();
console.log(s3 === s4); // true
复制代码
透明版解决了简单版不够“透明”的问题,又可使用 new
操做符来建立实例对象,瞬间以为天是蓝色,这个颜色真美,看谁也都顺眼了。
4.javascript 代理版
// 0.0.3/Singleton3.js
let SingletonProxy = (function () {
let instance
function main(name) {
if (!instance) {
return instance = new Singleton(name);
}
return instance;
}
return main
})();
let Singleton = function (name) {
this.name = name;
}
Singleton.prototype.show = function () {
console.log(this.name);
}
// 实例
const p1 = new SingletonProxy('代理1');
p1.show(); // 代理1
const p2 = new SingletonProxy('代理2');
p2.show(); // 代理1
console.log(p1 === p2); // true
复制代码
将管理单例操做,与对象建立操做进行拆分,实现更小的粒度划分,符合“单一职责原则”。
1.模态框(登陆框,信息提高框)
// 0.0.3/SingletonModal.js
class Modal {
constructor() {
this.display = 'hide';
}
show() {
if (this.display === 'show') {
console.log('不可重复展现');
return
}
this.display = 'show';
console.log('成功展现');
}
hide() {
if (this.display === 'hide') {
console.log('不可重复隐藏');
return
}
this.display = 'hide';
console.log('成功隐藏');
}
}
Modal.getInstance = (function () {
let instance = null
return function () {
if (instance === null) {
instance = new Modal();
}
return instance;
}
})();
// 实例
let m1 = Modal.getInstance();
let m2 = Modal.getInstance();
m1.show();
m2.show();
m1.hide();
m2.hide();
console.log(m1 === m2);
复制代码
2.其余
项目中引入第三方库时,重复屡次加载库文件时,全局只会实例化一个库对象,如 jQuery,lodash,moment ...
, 其实它们的实现理念也是单例模式应用的一种:
// 引入代码库 libs(库别名)
if (window.libs != null) {
return window.libs; // 直接返回
} else {
window.libs = '...'; // 初始化
}
复制代码