JAVA设计模式之备忘录模式原理与用法详解

2025-05-29 0 72

本文实例讲述了JAVA设计模式备忘录模式。分享给大家供大家参考,具体如下:

备忘录模式:又叫做快照模式,指在不破坏封装性的前提下,获取到一个对象的内部状态,并在对象之外记录或保存这个状态。在有需要的时候可将该对象恢复到原先保存的状态。我们相当于把对象原始状备份保留,所以叫备忘录模式

*模式 角色对象组成:

1.发起者对象:负责创建一个备忘录来记录当前对象的内部状态,并可使用备忘录恢复内部状态。

2.备忘录对象:负责存储发起者对象的内部状态,并防止其他对象访问备忘录。

3.管理者对象:负责备忘录权限管理,不能对备忘录对象的内容进行访问或者操作。

优点:

1、备忘录模式可以把发起人内部信息对象屏蔽起来,从而可以保持封装的边界。

2、简化了发起人类。当发起人角色的状态改变的时候,有可能这个状态无效,这时候就可以使用暂时存储起来的备忘录将状态复原。

缺点:

1、如果状态需要完整地存储到备忘录对象中,那么在资源消耗上面备忘录对象比较昂贵。

2、当发起者对象的状态改变的时候,有可能这个协议无效。如果状态改变的成功率达不到要求,可以考虑采取“假如”协议模式。

?

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
/**

* 备忘录对象类

* @description:

* @date 2016-1-22 上午11:15:59

*/

public class MemoBean {

private int useTime;//使用时间

private String deviceName;//设备名称

private int stateLevel;//状态

public int getUseTime() {

return useTime;

}

public void setUseTime(int useTime) {

this.useTime = useTime;

}

public String getDeviceName() {

return deviceName;

}

public void setDeviceName(String deviceName) {

this.deviceName = deviceName;

}

public int getStateLevel() {

return stateLevel;

}

public void setStateLevel(int stateLevel) {

this.stateLevel = stateLevel;

}

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
/**

* 备忘录管理对象

* @description:

* @date 2016-1-22 上午11:15:25

*/

public class MemoManager {

MemoBean memento;

public MemoBean getMemento() {

return memento;

}

public void setMemento(MemoBean memento) {

this.memento = memento;

}

}

?

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
/**

* 发起者对象

* @description:

* @date 2016-1-22 上午11:21:18

*/

public class MemoRole {

private int useTime;// 使用时间

private String deviceName;// 设备名称

private int stateLevel;// 状态

public MemoRole(String deviceName, int useTime, int stateLevel) {

super();

this.useTime = useTime;

this.deviceName = deviceName;

this.stateLevel = stateLevel;

}

public MemoRole() {

}

public int getUseTime() {

return useTime;

}

public void setUseTime(int useTime) {

this.useTime = useTime;

}

public String getDeviceName() {

return deviceName;

}

public void setDeviceName(String deviceName) {

this.deviceName = deviceName;

}

public int getStateLevel() {

return stateLevel;

}

public void setStateLevel(int stateLevel) {

this.stateLevel = stateLevel;

}

public MemoBean createMemoObject() {

MemoBean memento = new MemoBean();

memento.setDeviceName(deviceName);

memento.setStateLevel(stateLevel);

memento.setUseTime(useTime);

return memento;

}

public void setMemento(MemoBean memento) {

this.deviceName = memento.getDeviceName();

this.stateLevel = memento.getStateLevel();

this.useTime = memento.getUseTime();

}

/**

* 获取对象当前状态

* @description:

* @author ldm

* @date 2016-1-22 下午12:15:09

*/

public void getCurrentState() {

System.out.println("当前设备名称:" + this.deviceName + "当前使用时间:" + this.useTime + "当前工作状态:" + this.stateLevel);

}

}

测试类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
public class Test {

public static void main(String[] args) {

// 新建备忘录发起者对象

MemoRole role = new MemoRole("发电机", 0, 1);

// 新建备忘录管理者

MemoManager manager = new MemoManager();

// 角色初始状态

System.out.println("机器开始发电:");

role.getCurrentState();

// 利用备忘录模式保存当前状态

System.out.println("---保存当前的机器状态---");

manager.setMemento(role.createMemoObject());

role.setDeviceName("发电机");

role.setStateLevel(5);

role.setUseTime(1000);

System.out.println("已经持续发电1000小时");

role.getCurrentState();

// 恢复保存的角色状态

role.setMemento(manager.getMemento());

System.out.println("恢复后发电机当前状态:");

role.getCurrentState();

}

}

结果:

?

1

2

3

4

5

6

7
机器开始发电:

当前设备名称:发电机当前使用时间:0 当前工作状态:1

—保存当前的机器状态—

已经持续发电N小时

当前设备名称:发电机当前使用时间:1000 当前工作状态:5

恢复后发电机当前状态:

当前设备名称:发电机当前使用时间:0 当前工作状态1

运行结果的最后一句表示回到了初始状态,起到了备份作用。

希望本文所述对大家java程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 JAVA设计模式之备忘录模式原理与用法详解 https://www.kuaiidc.com/115145.html

相关文章

发表评论
暂无评论