Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 25a363d

Browse files
author
deeper
committed
add memento
1 parent d64c9da commit 25a363d

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

‎src/com/ruoxu/pattern/memento/CF.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.ruoxu.pattern.memento;
2+
/**
3+
* 穿越火线
4+
*/
5+
public class CF {
6+
private int mCheckPoint = 1;
7+
private int mLifeValue = 100;
8+
private String mWeapon = "沙漠之鹰";
9+
10+
public void play(){
11+
System.out.println("玩游戏 :"+String.format("第 %d 关", mCheckPoint) + "奋战杀敌中");
12+
mLifeValue -= 10;
13+
System.out.println("进度升级了");
14+
mCheckPoint ++;
15+
System.out.println("到达 "+ String.format("第%d关", mCheckPoint));
16+
}
17+
// 退出游戏
18+
public void quit(){
19+
System.out.println("========");
20+
System.out.println("退出前的游戏属性 :"+ this.toString());
21+
System.out.println("quit");
22+
System.out.println("========");
23+
}
24+
25+
public Memento createMemoto(){
26+
Memento memoto = new Memento();
27+
memoto.mCheckPoint = mCheckPoint;
28+
memoto.mLifeValue = mLifeValue;
29+
memoto.mWeapon = mWeapon;
30+
return memoto;
31+
}
32+
33+
// 恢复游戏
34+
public void restore(Memento memoto){
35+
this.mCheckPoint = memoto.mCheckPoint;
36+
this.mLifeValue = memoto.mLifeValue;
37+
this.mWeapon = memoto.mWeapon;
38+
System.out.println("恢复后的游戏属性:"+this.toString());
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "CF [mCheckPoint=" + mCheckPoint + ", mLifeValue=" + mLifeValue + ", mWeapon=" + mWeapon + "]";
44+
}
45+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.ruoxu.pattern.memento;
2+
3+
public class Caretaker {
4+
Memento mMemoto; // 备忘录
5+
/**
6+
* 存档
7+
*/
8+
public void archive(Memento memoto){
9+
this.mMemoto = memoto;
10+
}
11+
/**
12+
* 获取存档
13+
*/
14+
public Memento getMemoto(){
15+
return mMemoto;
16+
}
17+
}

‎src/com/ruoxu/pattern/memento/Demo.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.ruoxu.pattern.memento;
2+
/**
3+
* 备忘录模式
4+
* 定义:在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样,以后就可将该对象恢复到原先保存的状态。
5+
* 使用场景:
6+
* 1.需要保存一个对象在某一时刻的状态或部分状态
7+
* 2.如果用一个接口来让其他对象得到这些状态,将会暴露对象的实现细节并破坏对象的封装性,一个对象不希望外界直接访问其内部状态,通过中间对象可以间接访问其内部状态。
8+
*/
9+
public class Demo {
10+
public static void main(String[] args) {
11+
CF cf = new CF();
12+
cf.play();
13+
14+
// 游戏存档
15+
Caretaker caretaker = new Caretaker();
16+
caretaker.archive(cf.createMemoto());
17+
18+
cf.quit();
19+
20+
// 恢复游戏
21+
CF newGame = new CF();
22+
newGame.restore(caretaker.getMemoto());
23+
24+
}
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ruoxu.pattern.memento;
2+
3+
public class Memento {
4+
5+
public int mCheckPoint;
6+
public int mLifeValue;
7+
public String mWeapon;
8+
9+
@Override
10+
public String toString() {
11+
return "Memoto [mCheckPoint=" + mCheckPoint + ", mLifeValue=" + mLifeValue + ", mWeapon=" + mWeapon + "]";
12+
}
13+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /