Skip to content

Commit d508bd6

Browse files
committed
Memento Pattern
1 parent 8cfaa78 commit d508bd6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Example/Memento-Pattern.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
// 备忘录模式(Memento Pattern)
1+
// 备忘录模式(Memento Pattern)
2+
3+
let getPageData = (() => {
4+
let cache = {}
5+
return (page, fn) => {
6+
if (cache[page]) {
7+
// 已有指定缓存数据
8+
fun && fn()
9+
} else {
10+
// 当前没有指定缓存数据
11+
// 获取指定数据data
12+
cache[page] = data
13+
fn && fn()
14+
}
15+
}
16+
}
17+
)()
18+
19+
getPageData(1, () => {
20+
// do somthing
21+
})

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@
107107

108108
> [备忘录模式(Memento Pattern)](/Example/Memento-Pattern.js)
109109
110+
定义:保存一个对象的某个状态,以便在适当的时候恢复对象。
111+
目的:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。
112+
场景:数据缓存。
113+
110114
> [迭代器模式(Iterator Pattern)](/Example/Iterator-Pattern.js)
111115
112116
> [命令模式(Command Pattern)](/Example/Command-Pattern.js)

0 commit comments

Comments
 (0)