Skip to content

Commit cab2aed

Browse files
committed
添加备忘录模式代码
1 parent 6cb6e48 commit cab2aed

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

src/Memento(备忘录)/README.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@
99
-- ���ݿ������ϵͳ�ı��ݲ���
1010
-- ������Ϸ�еĻ��幦��
1111
���ŵ㣺
12+
-- �ṩ��һ�ֿ��Իָ�״̬�Ļ��ơ����û���Ҫʱ�ܹ��ȽϷ���ؽ����ݻָ���ij����ʷ��״̬��
13+
-- ʵ�����ڲ�״̬�ķ�װ�����˴������ķ�����֮�⣬�������󶼲��ܹ�������Щ״̬��Ϣ��
14+
-- ���˷������ࡣ�����˲���Ҫ�����ͱ������ڲ�״̬�ĸ������ݣ�����״̬��Ϣ�������ڱ���¼�У����ɹ����߽��й���������ϵ�һְ��ԭ��
1215
��ȱ�㣺
16+
-- ��Դ���Ĵ����Ҫ������ڲ�״̬��Ϣ��������ر�Ƶ��������ռ�ñȽϴ���ڴ���Դ��

src/Memento(备忘录)/common.java

Whitespace-only changes.

src/Memento(备忘录)/common.js

Whitespace-only changes.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
//备忘录
3+
class Memento {
4+
private state: string;
5+
public constructor(state: string) {
6+
this.state = state;
7+
}
8+
public setState(state: string): void {
9+
this.state = state;
10+
}
11+
public getState(): string {
12+
return this.state;
13+
}
14+
}
15+
//发起人
16+
class Originator {
17+
private state: string;
18+
public setState(state: string): void {
19+
this.state = state;
20+
}
21+
public getState(): string {
22+
return this.state;
23+
}
24+
public createMemento(): Memento {
25+
return new Memento(this.state);
26+
}
27+
public restoreMemento(m: Memento): void {
28+
this.setState(m.getState());
29+
}
30+
}
31+
//管理者
32+
class Caretaker {
33+
private memento: Memento;
34+
public setMemento(m: Memento): void {
35+
this.memento = m;
36+
}
37+
public getMemento(): Memento {
38+
return this.memento;
39+
}
40+
}
41+
42+
43+
class Client {
44+
public static main(): void {
45+
const or: Originator = new Originator();
46+
const cr: Caretaker = new Caretaker();
47+
or.setState("S0");
48+
console.log("初始状态:" + or.getState());
49+
cr.setMemento(or.createMemento()); //保存状态
50+
or.setState("S1");
51+
console.log("新的状态:" + or.getState());
52+
or.restoreMemento(cr.getMemento()); //恢复状态
53+
console.log("恢复状态:" + or.getState());
54+
}
55+
}
56+
Client.main()
57+
58+
// 初始状态:S0
59+
// 新的状态:S1
60+
// 恢复状态:S0

src/Memento(备忘录)/demo.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// 备忘录类
2+
class Memento {
3+
private x: number;
4+
private y: number;
5+
constructor(x: number, y: number) {
6+
this.x = x;
7+
this.y = y;
8+
}
9+
getX(): number {
10+
return this.x;
11+
}
12+
getY(): number {
13+
return this.y;
14+
}
15+
}
16+
17+
// 原发器类
18+
class Role {
19+
private x: number;
20+
private y: number;
21+
constructor(name: string, x: number, y: number) {
22+
this.x = x;
23+
this.y = y;
24+
}
25+
// 移动到新的位置
26+
moveTo(x: number, y: number): Memento {
27+
this.x = x;
28+
this.y = y;
29+
return this.save();
30+
}
31+
save(): Memento {
32+
return new Memento(this.x, this.y);
33+
}
34+
// 根据备忘录回退到某一个位置
35+
goBack(memento: Memento) {
36+
this.x = memento.getX();
37+
this.y = memento.getY();
38+
}
39+
}
40+
41+
// 负责人,管理所有备忘录
42+
class HistoryRecords {
43+
private records = [];
44+
// 添加备忘录
45+
add(record: Memento): void {
46+
this.records.push(record);
47+
}
48+
// 返回备忘录
49+
get(index: number): Memento {
50+
if (this.records[index]) {
51+
return this.records[index];
52+
}
53+
return null;
54+
}
55+
// 清除指定位置后面的备忘录
56+
cleanRecordsAfter(index: number): void {
57+
this.records.slice(0, index + 1);
58+
}
59+
}
60+
61+
62+
class Client {
63+
public static main(): void {
64+
const role = new Role('卡通小人', 0, 0);
65+
const records = new HistoryRecords();
66+
// 记录初始位置
67+
records.add(role.save());
68+
// 移动时添加备忘录
69+
role.moveTo(10, 10);
70+
records.add(role.save());
71+
role.moveTo(20, 30);
72+
records.add(role.save());
73+
// 回退到初始位置
74+
const GO_BACK_STEP = 0;
75+
const firstMemento = records.get(GO_BACK_STEP);
76+
role.goBack(firstMemento);
77+
// 清除后面的记录
78+
records.cleanRecordsAfter(GO_BACK_STEP);
79+
}
80+
}
81+
Client.main()
82+

0 commit comments

Comments
 (0)