设计模式学习笔记(36)——备忘录模式及其实现

本文深入探讨了备忘录设计模式的实现与应用,通过游戏存档实例详细讲解了如何在不破坏封装性的前提下,保存和恢复对象的内部状态。备忘录模式通过Originator、Memento和Caretaker三个角色,有效地解决了状态恢复的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

备忘录模式及其实现
一、游戏存进度

package operation;

class GameRole{
	private int vit;
	public int Vitality;
	public int Getvit() {
		return vit;
	}
	public void Setvit(int vit) {
		this.vit=vit;
	}
	private int atk;
	public int GetAttack(){
		return atk;
	}
	public void SetAttack(int  atk) {
		this.atk=atk;
	}
	private int def;
	public int GetDefense() {
		return def;
	}
	public void SetDefense(int def) {
		this.def=def;
	}
	public void StateDisplay() {
		System.out.println("角色当前的状态:");
		System.out.println("体力:"+Getvit());
		System.out.println("攻击力:"+GetAttack());
		System.out.println("防御力:"+GetDefense());
		System.out.println("");
	}
	public void GetInitState()
	{
		this.vit=100;
		this.atk=100;
		this.def=100;
	}
	public void Fight() {
		this.vit=0;
		this.atk=0;
		this.def=0;
	}
	}
public  class Main{
	public static void main(String[] args){
		GameRole lixiaoyao=new GameRole();
		lixiaoyao.GetInitState();
		lixiaoyao.StateDisplay();
		GameRole Backup=new GameRole();
		Backup.SetAttack(lixiaoyao.GetAttack());
		Backup.Setvit(lixiaoyao.Getvit());
		Backup.SetDefense(lixiaoyao.GetDefense());
		lixiaoyao.Fight();
	lixiaoyao.StateDisplay();
	lixiaoyao.Setvit(Backup.Getvit());
	lixiaoyao.SetAttack(Backup.GetAttack());
	lixiaoyao.SetDefense(Backup.GetDefense());
	lixiaoyao.StateDisplay();
	}}

以下

GameRole Backup=new GameRole();
		Backup.SetAttack(lixiaoyao.GetAttack());
		Backup.Setvit(lixiaoyao.Getvit());
		Backup.SetDefense(lixiaoyao.GetDefense());
		lixiaoyao.Fight();
	lixiaoyao.StateDisplay();
	lixiaoyao.Setvit(Backup.Getvit());
	lixiaoyao.SetAttack(Backup.GetAttack());
	lixiaoyao.SetDefense(Backup.GetDefense());
	lixiaoyao.StateDisplay();

暴露了细节,不可取。
二、备忘录模式
在这里插入图片描述Originator(发起人):负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。Originator可根据需要决定Memento存储Originator的哪些内部状态。
Memento(备忘录):负责存储Originator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录的窄接口,它只能将备忘录传递给他对象。Originator能够看到一个宽接口,允许它访问回到先前状态所需的所有数据。
Caretaker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查。
三、备忘录模式基本代码
备忘录:在不破坏封装性的前提下,铺获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态。

package operation;

class Originator{
	private String state;
 
	public String GetState() {
		return state;
	}
 
	public void SetState(String state) {
		this.state = state;
	}
	public Memento CreateMemento() {
		
		return(new Memento(state));
	}
	public void SetMemento(Memento memento) {
		
		state=memento.GetState();
	}
	public void Show() {
		System.out.println("State="+state);
	}
}
class Memento{
	private String state;
	
	public Memento(String state) {
		super();
		this.state = state;
	}
 
	public String GetState() {
		return state;
	}
 
	public void SetState(String state) {
		this.state = state;
	}
	
}
class Caretaker{
	private Memento memento;
 
	public Memento GetMemento() {
		return memento;
	}
 
	public void SetMemento(Memento memento) {
		this.memento = memento;
	}
	
}
public  class Main{
	public static void main(String[] args){
	Originator o=new Originator();
	o.SetState("On");
	o.Show();
	Caretaker c=new Caretaker();
	c.SetMemento(o.CreateMemento());
	o.SetState("Off");
	o.Show();
	o.SetMemento(c.GetMemento());
	o.Show();
	}
}

就是把要保存的细节封装在了Memento中了,哪一天要更改保存的细节也不用影响客户端了。

四、游戏进度备忘
在这里插入图片描述

package operation;

class GameRole{
	private int vit;
	public int Vitality;
	public int Getvit() {
		return vit;
	}
	public void Setvit(int vit) {
		this.vit=vit;
	}
	private int atk;
	public int GetAttack(){
		return atk;
	}
	public void SetAttack(int  atk) {
		this.atk=atk;
	}
	private int def;
	public int GetDefense() {
		return def;
	}
	public void SetDefense(int def) {
		this.def=def;
	}
	public void StateDisplay() {
		System.out.println("角色当前的状态:");
		System.out.println("体力:"+Getvit());
		System.out.println("攻击力:"+GetAttack());
		System.out.println("防御力:"+GetDefense());
		System.out.println("");
	}
	public void GetInitState()
	{
		this.vit=100;
		this.atk=100;
		this.def=100;
	}
	public void Fight() {
		this.vit=0;
		this.atk=0;
		this.def=0;
	}
	public RoleStateMemento SaveRoleState() {//存档状态
		return new RoleStateMemento(this.vit,this.atk,this.def);
	}

	public void RecoveryState(RoleStateMemento memento) {
		this.vit=memento.Getvit();
		this.atk=memento.GetAttack();
		this.def=memento.GetDefense();
	}

	}
class RoleStateMemento{
	private int vit;
	
	public int Getvit() {
		return vit;
	}
	public void Setvit(int vit) {
		this.vit=vit;
	}
	private int atk;
	public int GetAttack(){
		return atk;
	}
	public void SetAttack(int  atk) {
		this.atk=atk;
	}
	private int def;
	public int GetDefense() {
		return def;
	}
	public void SetDefense(int def) {
		this.def=def;
	}

public RoleStateMemento(int vit, int atk, int def) {
	super();
	this.vit=vit;
	this.atk=atk;
	this.def=def;
}}
class RoleStateCaretaker{
	RoleStateMemento rolestatememento;
 
	public RoleStateMemento getRoleStateMemento() {
		return rolestatememento;
	}
 
	public void setRoleStateMemento(RoleStateMemento rolestatememento) {
		this.rolestatememento = rolestatememento;
	}
	
}
public  class Main{
	public static void main(String[] args){
	GameRole lixiaoyao=new GameRole();
	lixiaoyao.GetInitState();
	lixiaoyao.StateDisplay();

	RoleStateCaretaker caretaker=new RoleStateCaretaker();
	caretaker.setRoleStateMemento(lixiaoyao.SaveRoleState());

	lixiaoyao.Fight();
	lixiaoyao.StateDisplay();

	lixiaoyao.RecoveryState(caretaker.getRoleStateMemento());
	lixiaoyao.StateDisplay();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值