23种设计模式----备忘录模式----行为模式


23种设计模式
直达底部

1.什么是备忘录模式

现在许多的文本编辑器都有一些功能:
撤销,重做,历史记录等等。
显示生活中,遇到有意义的瞬间,大家就会拍照留念。
照片保存了那个瞬间的一些信息:所处的环境的信息,目标的信息。。。。

所以备忘录模式就是实现可以保存某个对象的状态信息的功能,保存之后在需要的情况下,随时可以让对象恢复到保存的状态。

2.角色说明

1.目标:被记录的对象
2.快照:保存的目标对象信息的类

3.关系说明

实现备忘录模式的核心思想:
首先,创建一个类,这个类根据被保存对象进行设计,这个类需要满足能够记录目标类的需要的信息这个条件。为了保护数据,这个新创建的类不能随意的被其他类访问,只能由目标类访问,其他类只有恢复的操作,不能有访问属性值的操作。

4.实现方式

首先快照类需要保证数据的安全性,所以快照类的属性应该是私有,这样即使其他的类继承快照类,也无法获取快照的属性值。
其次,因为目标类需要能够访问快照,所以快照类和目标需要在同一个包内:快照类设置自己的属性的访问方法的权限为包内。
如果目标类的属性值机密性高,那么可以吧目标类的属性访问方式设置为包内,这样只能由目标类和快照类相互访问。

方法的访问权限有包内这个还是有原因的。

5.例子

5.1文件结构

在这里插入图片描述

5.2项目介绍

本次做一个游戏:猜大小,很简单,但是要知道程序是我们写的,所以特权肯定要有了。所以本次猜大小游戏的特权就是每一句输了可以免单。怎么说呢,猜大小,赢了加5分,输了不加分(第二次输会扣分)。(最后结果还能输吗)

5.3骰子类

package dice;

import java.util.Random;

public class Dice {

	public static Integer getDice(){
		return new Random().nextInt(6) + 1;
	}
	
}

5.4玩家类

package player;

import java.util.Random;

public class Player {

	private Integer score;
	
	private String name;
	
	public Integer getScore() {
		return score;
	}

	public void setScore(Integer score) {
		this.score = score;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public boolean getplay(){
		return new Random().nextBoolean();
	}
	
	public Memento createMemento(){
		Memento memento = new Memento(name, score);
		return memento;
	}
	
}

5.5快照类

package player;

public class Memento {

	private String name;
	
	private Integer score;
	
	Memento(String name,Integer score) {
		this.name = name;
		this.score = score;
	}

	public Player reset(Player player,Memento memento){
		player.setName(memento.getName());
		player.setScore(memento.getScore());
		return player;
	}
	
	String getName() {
		return name;
	}

	void setName(String name) {
		this.name = name;
	}

	Integer getScore() {
		return score;
	}

	void setScore(Integer score) {
		this.score = score;
	}
	
}

5.6使用者

package client;

import java.util.Scanner;

import player.Memento;
import player.Player;
import dice.Dice;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("create player");
		Player player = new Player();
		System.out.println("please input name");
		player.setName(scanner.next());
		System.out.println(player.getName()+"player score is 0");
		player.setScore(0);
		
		System.out.println("it will be 100 times game");
		System.out.println("game start");
		
		System.out.println("create mem");
		Memento memento = player.createMemento();
		
		int sum = 0;
		
		for(int i = 0;i < 100;i++){
			System.out.println((i+1)+"'s gamer");
			boolean b = player.getplay();
			System.out.println(player.getName()+"player is "+(b ? "大" : "小"));
			int d = Dice.getDice();
			System.out.println("the dice is "+d);
			System.out.println("so the result is "
					+ ((d > 3 ? true : false) ? "大" : "小"));
			boolean res = (d > 3 && b) || (d <= 3 && !b);
			System.out.println((i+1)+"'s gamer is "+res);
			if(!res){
				System.out.println(player.getName()+"is false ,so please sub score");
				player.setScore(player.getScore() - 5 >= 0 ? player.getScore() - 5 : 0);
				System.out.println(player.getName()+"sub score is " + player.getScore());
				System.out.println("reset mem");
				player = memento == null ? player : memento.reset(player, memento);
				memento = null;
			} else {
				System.out.println(player.getName()+"is true ,so please add score");
				player.setScore(player.getScore() + 5);
				System.out.println(player.getName() + "'s score is"+player.getScore());
				System.out.println("save mem");
				memento = player.createMemento();
				sum++;
			}
		}
		System.out.println("now is over");
		
		System.out.println();
		System.out.println(player.getName()+" player have "+sum+"times game is true ");
		
	}

}

5.7结果

create player
please input name
lili
liliplayer score is 0
it will be 100 times game
game start
create mem
1's gamer
liliplayer is 大
the dice is 1
so the result is 小
1's gamer is false
liliis false ,so please sub score
lilisub score is 0
reset mem
2's gamer
liliplayer is 大
the dice is 2
so the result is 小
2's gamer is false
liliis false ,so please sub score
lilisub score is 0
reset mem
3's gamer
liliplayer is 小
the dice is 3
so the result is 小
3's gamer is true
liliis true ,so please add score
lili's score is5
save mem
4's gamer
liliplayer is 小
the dice is 6
so the result is 大
4's gamer is false
liliis false ,so please sub score
lilisub score is 0
reset mem
5's gamer
liliplayer is 小
the dice is 4
so the result is 大
5's gamer is false
liliis false ,so please sub score
lilisub score is 0
reset mem
6's gamer
liliplayer is 小
the dice is 6
so the result is 大
6's gamer is false
liliis false ,so please sub score
lilisub score is 0
reset mem
7's gamer
liliplayer is 小
the dice is 2
so the result is 小
7's gamer is true
liliis true ,so please add score
lili's score is5
save mem
8's gamer
liliplayer is 小
the dice is 4
so the result is 大
8's gamer is false
liliis false ,so please sub score
lilisub score is 0
reset mem
9's gamer
liliplayer is 大
the dice is 5
so the result is 大
9's gamer is true
liliis true ,so please add score
lili's score is10
save mem
10's gamer
liliplayer is 大
the dice is 4
so the result is 大
10's gamer is true
liliis true ,so please add score
lili's score is15
save mem
11's gamer
liliplayer is 小
the dice is 6
so the result is 大
11's gamer is false
liliis false ,so please sub score
lilisub score is 10
reset mem
12's gamer
liliplayer is 小
the dice is 4
so the result is 大
12's gamer is false
liliis false ,so please sub score
lilisub score is 10
reset mem
13's gamer
liliplayer is 小
the dice is 2
so the result is 小
13's gamer is true
liliis true ,so please add score
lili's score is15
save mem
14's gamer
liliplayer is 小
the dice is 5
so the result is 大
14's gamer is false
liliis false ,so please sub score
lilisub score is 10
reset mem
15's gamer
liliplayer is 小
the dice is 6
so the result is 大
15's gamer is false
liliis false ,so please sub score
lilisub score is 10
reset mem
16's gamer
liliplayer is 小
the dice is 6
so the result is 大
16's gamer is false
liliis false ,so please sub score
lilisub score is 5
reset mem
17's gamer
liliplayer is 小
the dice is 2
so the result is 小
17's gamer is true
liliis true ,so please add score
lili's score is10
save mem
18's gamer
liliplayer is 小
the dice is 1
so the result is 小
18's gamer is true
liliis true ,so please add score
lili's score is15
save mem
19's gamer
liliplayer is 小
the dice is 5
so the result is 大
19's gamer is false
liliis false ,so please sub score
lilisub score is 10
reset mem
20's gamer
liliplayer is 小
the dice is 2
so the result is 小
20's gamer is true
liliis true ,so please add score
lili's score is20
save mem
21's gamer
liliplayer is 小
the dice is 5
so the result is 大
21's gamer is false
liliis false ,so please sub score
lilisub score is 15
reset mem
22's gamer
liliplayer is 小
the dice is 4
so the result is 大
22's gamer is false
liliis false ,so please sub score
lilisub score is 15
reset mem
23's gamer
liliplayer is 小
the dice is 4
so the result is 大
23's gamer is false
liliis false ,so please sub score
lilisub score is 10
reset mem
24's gamer
liliplayer is 大
the dice is 5
so the result is 大
24's gamer is true
liliis true ,so please add score
lili's score is15
save mem
25's gamer
liliplayer is 大
the dice is 6
so the result is 大
25's gamer is true
liliis true ,so please add score
lili's score is20
save mem
26's gamer
liliplayer is 小
the dice is 5
so the result is 大
26's gamer is false
liliis false ,so please sub score
lilisub score is 15
reset mem
27's gamer
liliplayer is 小
the dice is 3
so the result is 小
27's gamer is true
liliis true ,so please add score
lili's score is25
save mem
28's gamer
liliplayer is 大
the dice is 3
so the result is 小
28's gamer is false
liliis false ,so please sub score
lilisub score is 20
reset mem
29's gamer
liliplayer is 大
the dice is 4
so the result is 大
29's gamer is true
liliis true ,so please add score
lili's score is30
save mem
30's gamer
liliplayer is 小
the dice is 4
so the result is 大
30's gamer is false
liliis false ,so please sub score
lilisub score is 25
reset mem
31's gamer
liliplayer is 小
the dice is 2
so the result is 小
31's gamer is true
liliis true ,so please add score
lili's score is35
save mem
32's gamer
liliplayer is 小
the dice is 1
so the result is 小
32's gamer is true
liliis true ,so please add score
lili's score is40
save mem
33's gamer
liliplayer is 小
the dice is 3
so the result is 小
33's gamer is true
liliis true ,so please add score
lili's score is45
save mem
34's gamer
liliplayer is 小
the dice is 5
so the result is 大
34's gamer is false
liliis false ,so please sub score
lilisub score is 40
reset mem
35's gamer
liliplayer is 大
the dice is 3
so the result is 小
35's gamer is false
liliis false ,so please sub score
lilisub score is 40
reset mem
36's gamer
liliplayer is 大
the dice is 1
so the result is 小
36's gamer is false
liliis false ,so please sub score
lilisub score is 35
reset mem
37's gamer
liliplayer is 大
the dice is 6
so the result is 大
37's gamer is true
liliis true ,so please add score
lili's score is40
save mem
38's gamer
liliplayer is 大
the dice is 2
so the result is 小
38's gamer is false
liliis false ,so please sub score
lilisub score is 35
reset mem
39's gamer
liliplayer is 大
the dice is 2
so the result is 小
39's gamer is false
liliis false ,so please sub score
lilisub score is 35
reset mem
40's gamer
liliplayer is 小
the dice is 6
so the result is 大
40's gamer is false
liliis false ,so please sub score
lilisub score is 30
reset mem
41's gamer
liliplayer is 小
the dice is 5
so the result is 大
41's gamer is false
liliis false ,so please sub score
lilisub score is 25
reset mem
42's gamer
liliplayer is 小
the dice is 6
so the result is 大
42's gamer is false
liliis false ,so please sub score
lilisub score is 20
reset mem
43's gamer
liliplayer is 小
the dice is 2
so the result is 小
43's gamer is true
liliis true ,so please add score
lili's score is25
save mem
44's gamer
liliplayer is 大
the dice is 5
so the result is 大
44's gamer is true
liliis true ,so please add score
lili's score is30
save mem
45's gamer
liliplayer is 大
the dice is 5
so the result is 大
45's gamer is true
liliis true ,so please add score
lili's score is35
save mem
46's gamer
liliplayer is 小
the dice is 1
so the result is 小
46's gamer is true
liliis true ,so please add score
lili's score is40
save mem
47's gamer
liliplayer is 小
the dice is 2
so the result is 小
47's gamer is true
liliis true ,so please add score
lili's score is45
save mem
48's gamer
liliplayer is 大
the dice is 3
so the result is 小
48's gamer is false
liliis false ,so please sub score
lilisub score is 40
reset mem
49's gamer
liliplayer is 大
the dice is 5
so the result is 大
49's gamer is true
liliis true ,so please add score
lili's score is50
save mem
50's gamer
liliplayer is 小
the dice is 6
so the result is 大
50's gamer is false
liliis false ,so please sub score
lilisub score is 45
reset mem
51's gamer
liliplayer is 小
the dice is 5
so the result is 大
51's gamer is false
liliis false ,so please sub score
lilisub score is 45
reset mem
52's gamer
liliplayer is 大
the dice is 1
so the result is 小
52's gamer is false
liliis false ,so please sub score
lilisub score is 40
reset mem
53's gamer
liliplayer is 小
the dice is 1
so the result is 小
53's gamer is true
liliis true ,so please add score
lili's score is45
save mem
54's gamer
liliplayer is 大
the dice is 3
so the result is 小
54's gamer is false
liliis false ,so please sub score
lilisub score is 40
reset mem
55's gamer
liliplayer is 大
the dice is 5
so the result is 大
55's gamer is true
liliis true ,so please add score
lili's score is50
save mem
56's gamer
liliplayer is 大
the dice is 6
so the result is 大
56's gamer is true
liliis true ,so please add score
lili's score is55
save mem
57's gamer
liliplayer is 大
the dice is 5
so the result is 大
57's gamer is true
liliis true ,so please add score
lili's score is60
save mem
58's gamer
liliplayer is 小
the dice is 2
so the result is 小
58's gamer is true
liliis true ,so please add score
lili's score is65
save mem
59's gamer
liliplayer is 小
the dice is 6
so the result is 大
59's gamer is false
liliis false ,so please sub score
lilisub score is 60
reset mem
60's gamer
liliplayer is 小
the dice is 1
so the result is 小
60's gamer is true
liliis true ,so please add score
lili's score is70
save mem
61's gamer
liliplayer is 小
the dice is 6
so the result is 大
61's gamer is false
liliis false ,so please sub score
lilisub score is 65
reset mem
62's gamer
liliplayer is 大
the dice is 2
so the result is 小
62's gamer is false
liliis false ,so please sub score
lilisub score is 65
reset mem
63's gamer
liliplayer is 大
the dice is 3
so the result is 小
63's gamer is false
liliis false ,so please sub score
lilisub score is 60
reset mem
64's gamer
liliplayer is 大
the dice is 1
so the result is 小
64's gamer is false
liliis false ,so please sub score
lilisub score is 55
reset mem
65's gamer
liliplayer is 大
the dice is 4
so the result is 大
65's gamer is true
liliis true ,so please add score
lili's score is60
save mem
66's gamer
liliplayer is 大
the dice is 1
so the result is 小
66's gamer is false
liliis false ,so please sub score
lilisub score is 55
reset mem
67's gamer
liliplayer is 小
the dice is 4
so the result is 大
67's gamer is false
liliis false ,so please sub score
lilisub score is 55
reset mem
68's gamer
liliplayer is 大
the dice is 4
so the result is 大
68's gamer is true
liliis true ,so please add score
lili's score is60
save mem
69's gamer
liliplayer is 大
the dice is 6
so the result is 大
69's gamer is true
liliis true ,so please add score
lili's score is65
save mem
70's gamer
liliplayer is 大
the dice is 1
so the result is 小
70's gamer is false
liliis false ,so please sub score
lilisub score is 60
reset mem
71's gamer
liliplayer is 小
the dice is 2
so the result is 小
71's gamer is true
liliis true ,so please add score
lili's score is70
save mem
72's gamer
liliplayer is 大
the dice is 3
so the result is 小
72's gamer is false
liliis false ,so please sub score
lilisub score is 65
reset mem
73's gamer
liliplayer is 大
the dice is 5
so the result is 大
73's gamer is true
liliis true ,so please add score
lili's score is75
save mem
74's gamer
liliplayer is 小
the dice is 3
so the result is 小
74's gamer is true
liliis true ,so please add score
lili's score is80
save mem
75's gamer
liliplayer is 小
the dice is 2
so the result is 小
75's gamer is true
liliis true ,so please add score
lili's score is85
save mem
76's gamer
liliplayer is 大
the dice is 1
so the result is 小
76's gamer is false
liliis false ,so please sub score
lilisub score is 80
reset mem
77's gamer
liliplayer is 大
the dice is 6
so the result is 大
77's gamer is true
liliis true ,so please add score
lili's score is90
save mem
78's gamer
liliplayer is 大
the dice is 1
so the result is 小
78's gamer is false
liliis false ,so please sub score
lilisub score is 85
reset mem
79's gamer
liliplayer is 大
the dice is 3
so the result is 小
79's gamer is false
liliis false ,so please sub score
lilisub score is 85
reset mem
80's gamer
liliplayer is 大
the dice is 2
so the result is 小
80's gamer is false
liliis false ,so please sub score
lilisub score is 80
reset mem
81's gamer
liliplayer is 大
the dice is 4
so the result is 大
81's gamer is true
liliis true ,so please add score
lili's score is85
save mem
82's gamer
liliplayer is 大
the dice is 4
so the result is 大
82's gamer is true
liliis true ,so please add score
lili's score is90
save mem
83's gamer
liliplayer is 小
the dice is 3
so the result is 小
83's gamer is true
liliis true ,so please add score
lili's score is95
save mem
84's gamer
liliplayer is 大
the dice is 4
so the result is 大
84's gamer is true
liliis true ,so please add score
lili's score is100
save mem
85's gamer
liliplayer is 小
the dice is 5
so the result is 大
85's gamer is false
liliis false ,so please sub score
lilisub score is 95
reset mem
86's gamer
liliplayer is 小
the dice is 6
so the result is 大
86's gamer is false
liliis false ,so please sub score
lilisub score is 95
reset mem
87's gamer
liliplayer is 大
the dice is 4
so the result is 大
87's gamer is true
liliis true ,so please add score
lili's score is100
save mem
88's gamer
liliplayer is 大
the dice is 6
so the result is 大
88's gamer is true
liliis true ,so please add score
lili's score is105
save mem
89's gamer
liliplayer is 大
the dice is 5
so the result is 大
89's gamer is true
liliis true ,so please add score
lili's score is110
save mem
90's gamer
liliplayer is 小
the dice is 4
so the result is 大
90's gamer is false
liliis false ,so please sub score
lilisub score is 105
reset mem
91's gamer
liliplayer is 大
the dice is 5
so the result is 大
91's gamer is true
liliis true ,so please add score
lili's score is115
save mem
92's gamer
liliplayer is 小
the dice is 2
so the result is 小
92's gamer is true
liliis true ,so please add score
lili's score is120
save mem
93's gamer
liliplayer is 大
the dice is 1
so the result is 小
93's gamer is false
liliis false ,so please sub score
lilisub score is 115
reset mem
94's gamer
liliplayer is 小
the dice is 5
so the result is 大
94's gamer is false
liliis false ,so please sub score
lilisub score is 115
reset mem
95's gamer
liliplayer is 小
the dice is 5
so the result is 大
95's gamer is false
liliis false ,so please sub score
lilisub score is 110
reset mem
96's gamer
liliplayer is 小
the dice is 2
so the result is 小
96's gamer is true
liliis true ,so please add score
lili's score is115
save mem
97's gamer
liliplayer is 大
the dice is 5
so the result is 大
97's gamer is true
liliis true ,so please add score
lili's score is120
save mem
98's gamer
liliplayer is 大
the dice is 3
so the result is 小
98's gamer is false
liliis false ,so please sub score
lilisub score is 115
reset mem
99's gamer
liliplayer is 大
the dice is 2
so the result is 小
99's gamer is false
liliis false ,so please sub score
lilisub score is 115
reset mem
100's gamer
liliplayer is 小
the dice is 4
so the result is 大
100's gamer is false
liliis false ,so please sub score
lilisub score is 110
reset mem
now is over

lili player have 47times game is true 

6.扩展

例子中每次只能保存一个快照。如果我们用一个list来保存,那么,我们可以任意回到某一局游戏时的状态。

直达顶部
23种设计模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a18792721831

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值