【设计模式】备忘录模式

备忘录模式

   在不破坏封装性的前提下,补货一个对象的内部状态,并在改对象之外保存这个状态,这样就可以将现在的状态恢复到原先保存的状态。

模型图

 代码

public class Originator {

	private String state="on";
	
	private Memento memento=null;

	public Memento getMemento() {
		return new Memento(state);
	}

	public void setMemento(Memento memento) {
		this.state=memento.getState();
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
	
	public void showState(){
		System.out.println("状态:="+this.state);
	}
}

 

public class Memento {
	private String state="on";

	public Memento(String state) {
         this.state=state;
	}
	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
}
public class CareTaker {
	private Memento memento;

	public Memento getMemento() {
		return memento;
	}

	public void setMemento(Memento memento) {
		this.memento = memento;
	}
}

 

public class Test {
   public static void main(String[] args) {
	Originator  originator=new Originator();
	originator.setState("on");
	originator.showState();
	
	
	CareTaker careTaker=new CareTaker();
	careTaker.setMemento(originator.getMemento());
    originator.setState("off");
    originator.showState();
    
    
    originator.setMemento(careTaker.getMemento());
    originator.showState();	
	
	   
   }
}

 

 

 

 案例 模拟游戏角色生命存档

分析设计 通过备忘录模式进行存档

模型图

 

 

public class RoleGame {
	//生命力
	private int live;
	//法力
	private int atk;
	//攻击力
	private int hit;
	
    private RoleStateMemento roleStateMemento=null;
    
	public RoleStateMemento getRoleStateMemento() {
		return new RoleStateMemento(live,atk,hit);
	}
	public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
		this.live=roleStateMemento.getLive();
		this.atk=roleStateMemento.getAtk();
		this.hit=roleStateMemento.getHit();
	}
	public int getLive() {
		return live;
	}
	public void setLive(int live) {
		this.live = live;
	}
	@Override
	public String toString() {
		return "RoleGame [live=" + live + ", atk=" + atk + ", hit=" + hit+"]";
	}
	public int getAtk() {
		return atk;
	}
	public RoleGame(int live, int atk, int hit) {
		super();
		this.live = live;
		this.atk = atk;
		this.hit = hit;
	}
	public void setAtk(int atk) {
		this.atk = atk;
	}
	public int getHit() {
		return hit;
	}
	public void setHit(int hit) {
		this.hit = hit;
	}
	
	
	public void show(){
		System.out.println(toString());
	}
	
	
}

 


public class RoleGameCareTaker {
    Map<String,RoleStateMemento> memen=new ConcurrentHashMap<String, RoleStateMemento>();
	
	public void savePro(String key,RoleStateMemento value){
		memen.put(key, value);
	}
	
	
	public RoleStateMemento readPro(String key){
		return memen.get(key);
	}
}
public class RoleStateMemento {
	public RoleStateMemento(int live, int atk, int hit) {
		super();
		this.live = live;
		this.atk = atk;
		this.hit = hit;
	}
	//生命力
	private int live;
	//法力
	private int atk;
	//攻击力
	private int hit;
		
	public int getLive() {
		return live;
	}
	public void setLive(int live) {
		this.live = live;
	}
	public int getAtk() {
		return atk;
	}
	public void setAtk(int atk) {
		this.atk = atk;
	}
	public int getHit() {
		return hit;
	}
	public void setHit(int hit) {
		this.hit = hit;
	}
		
}

 

 

public class Test {
 public static void main(String[] args) {
	RoleGame game=new RoleGame(1, 1, 1);
	game.show();
	
	RoleGameCareTaker careTaker=new RoleGameCareTaker();
	careTaker.savePro("1",game.getRoleStateMemento());
	
	game=new RoleGame(2, 2, 2);
	careTaker.savePro("2", game.getRoleStateMemento());
	game.show();
	
	game=new RoleGame(3, 3, 3);
	careTaker.savePro("3", game.getRoleStateMemento());
	game.show();
	
	game.setRoleStateMemento(careTaker.readPro("1"));
	game.show();
	
	game.setRoleStateMemento(careTaker.readPro("2"));
	game.show();
 }
}

结果图

 

 小结

1 将对象状态的细节保存在备忘录中 哪天更改备忘录也不至于影响到原对象。

2 适合功能比较复杂 又要保存历史属性的具体类

3 就是追溯历史变量的时候

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值