结构型-享元模式

概念

Flyweight Pattern:运用共享技术复用大量细粒度的对象,降低程序内存的占用,提高程序的性能。

img

享元模式提醒我们讲一个对象的属性划分为内部和外部状态

  • 内部状态:可以被对象集合共享,通常不会改变

  • 外部状态:根据应用场景经常改变

享元模式是利用时间换取空间的优化模式

使用

以下用Es6实现一个“通用对象池”类-ObjectPool,这个类管理一个装载空闲对象的数组,如果外部需要一个对象,直接从对象池中获取,而不是通过new操作;模拟File类,“文件下载”操作。

对于File类,内部状态是pool属性和download方法;外部状态是namesrc。借助对象池,实现了File类的复用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 对象池
class ObjectPool {
constructor() {
this._pool = []; //
}

// 创建对象
create(Obj) {
return this._pool.length === 0
? new Obj(this) // 对象池中没有空闲对象,则创建一个新的对象
: this._pool.shift(); // 对象池中有空闲对象,直接取出,无需再次创建
}

// 对象回收
recover(obj) {
return this._pool.push(obj);
}

// 对象池大小
size() {
return this._pool.length;
}
}

// 模拟文件对象
class File {
constructor(pool) {
this.pool = pool;
}

// 模拟下载操作
download() {
console.log(`+ 从 ${this.src} 开始下载 ${this.name}`);
setTimeout(() => {
console.log(`- ${this.name} 下载完毕`); // 下载完毕后, 将对象重新放入对象池
this.pool.recover(this);
}, 100);
}
}

/****************** 以下是测试函数 **********************/

let objPool = new ObjectPool();

let file1 = objPool.create(File);
file1.name = "文件1";
file1.src = "https://download1.com";
file1.download();

let file2 = objPool.create(File);
file2.name = "文件2";
file2.src = "https://download2.com";
file2.download();


let file3 = objPool.create(File);
file3.name = "文件3";
file3.src = "https://download3.com";
file3.download();


setTimeout(
() =>
console.log(
`${"*".repeat(50)}\n下载了3个文件,但其实只创建了${objPool.size()}个对象`
),
1000
);
// 因为js是并发操作,所以创建3个对象,假如把file3这样写,就是创建2个,
/*setTimeout(() => {
let file3 = objPool.create(File);
file3.name = "文件3";
file3.src = "https://download3.com";
file3.download();
}, 200);
*/

优点

  • 减少内存中对象的数量,使相同对象或相似对象在内存中只保存一份,降低系统的使用内存,提高性能
  • 可以使外部状态相对独立,而且不会影响其内部状态,从而使享元对象可以在不同的环境中被共享

缺点

  • 需要分离出内部状态和外部状态,使程序的逻辑复杂化
  • 对象在缓冲池中的复用需要考虑线程问题