Java graphical design mode of a memo mode

Game character state recovery problem

Game character attack and defense, to save their own state (attack and defense) before the war Boss, Boss after World War II attack and defense power drops, recovery from the memo object to the state before the war.

Traditional solutions to restore the role of the game

Here Insert Picture Description

The traditional way of analyzing the problem

1) an object, the corresponding object on a saved state of the object, so that when the object of the game when many of us, is not conducive to management, large overhead.
2) The traditional way is to simply do a backup, new out of another object out, then the data to be backed up into the new object, but this exposes the internal details of the object

Basic introduction memo mode

1) memo mode (Memento Pattern) without violating encapsulation, capture and the internal state of an object, and save the state outside the object. So that later you can restore the object to a previously saved state.
2) a memorandum of understanding here may modes: real life memo is used to record the views of some of the common things you want to do something, or has reached a record, in case you forget. In the software level, the model has the same meaning memo, the memo is primarily used to record a certain state of an object, or some of the data, when to do rollback, you can get the original data from the memo object in the recovery operation.
3) Memorandum pattern belongs behavioral patterns.

FIG principle memo mode class

Here Insert Picture Description
Class diagram illustrative of the principles:
. 1) Originator: Object (Object need to preserve state)
2) Memento: memo object, responsible for keeping good recording, i.e. Originator internal state
3) Caretaker: guarded object is responsible for storing a plurality of memo object, using the set management, improve the efficiency of
4) Note: If you want to save the state of a plurality of different times originator objects, may only need HashMap <String, set>

package com.example.demo.memento.theory;

public class Memento {
	
	private String state;
	
	public Memento(String state) {
		super();
		this.state = state;
	}

	public String getState() {
		return state;
	}

}
package com.example.demo.memento.theory;

import java.util.ArrayList;
import java.util.List;

public class Caretaker {
	
	/**
	 * 在list 集合中会有很多的备忘录对象
	 */
	private List<Memento> mementos = new ArrayList<Memento>();
	
	public void add(Memento memento) {
		mementos.add(memento);
	}
	
	/**
	 * 获取到第index个Originator 的 备忘录对象(即保存状态)
	 * @param index
	 * @return
	 */
	public Memento get(int index) {
		return mementos.get(index);
	}
}
package com.example.demo.memento.theory;

public class Originator {
	
	/**
	 * 状态信息
	 */
	private String state;

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
	
	// 编写一个方法,可以保存一个状态对象 Memento
	// 因此编写一个方法,返回Memento
	public Memento saveStateMemento() {
		return new Memento(state);
	}
	
	public void getStateFromMemento(Memento memento) {
		state = memento.getState();
	}

}
package com.example.demo.memento.theory;

public class Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Originator originator = new Originator();
		Caretaker caretaker = new Caretaker();
		originator.setState(" 状态1 攻击力100 ");
		// 保存当前状态
		caretaker.add(originator.saveStateMemento());
		
		originator.setState(" 状态2 攻击力80 ");
		// 保存当前状态
		caretaker.add(originator.saveStateMemento());
		
		originator.setState(" 状态3 攻击力50 ");
		// 保存当前状态
		caretaker.add(originator.saveStateMemento());
		System.out.println(" 现在状态是 " + originator.getState());
		
		// 希望得到状态1,将originator 恢复到状态1
		originator.getStateFromMemento(caretaker.get(0));
		System.out.println(" 恢复状态 1");
		System.out.println("当前的状态是 = " + originator.getState());
	}

}

Examples of game characters to restore state

1) Application Examples requirements
game character attack and defense, to save their own state (attack and defense) before the war Boss, after World War Boss attack and defense power drops, recovery from the memo object to the state before the war
2) class diagram
Here Insert Picture Description

package com.example.demo.memento.game;

public class Memento {
	
	/**
	 * 攻击力
	 */
	private int vit;

	/**
	 * 防御力
	 */
	private int def;

	public Memento(int vit, int def) {
		super();
		this.vit = vit;
		this.def = def;
	}

	public int getVit() {
		return vit;
	}

	public void setVit(int vit) {
		this.vit = vit;
	}

	public int getDef() {
		return def;
	}

	public void setDef(int def) {
		this.def = def;
	}
}
package com.example.demo.memento.game;

import java.util.List;
import java.util.Map;

import javax.activation.MailcapCommandMap;

/**
 * 守护者对象,保存游戏角色的状态
 * @author zhaozhaohai
 *
 */
public class Caretaker {

	/**
	 * 如果只保存一次状态
	 */
	private Memento memento;
	/**
	 * 对GameRole 保存多次状态
	 */
	private List<Memento> list;
	/**
	 * 对多个游戏角色保存多个状态
	 */
	private Map<String, List<Memento>> rMap;
	public Memento getMemento() {
		return memento;
	}
	public void setMemento(Memento memento) {
		this.memento = memento;
	}
	
}
package com.example.demo.memento.game;

public class GameRole {
	
	private int vit;
	
	private int def;
	
	/**
	 * 创建Memento,即根据当前的状态得到Memento
	 * @return
	 */
	public Memento createMemento() {
		return new Memento(vit, def);
	}
	
	/**
	 * 从备忘录对象,恢复GameRole的状态
	 * @param memento
	 */
	public void recoverGameRoleFromMemento(Memento memento) {
		this.vit = memento.getVit();
		this.def = memento.getDef();
	}
	
	/**
	 * 显示当前游戏角色的状态
	 */
	public void display() {
		System.out.println("游戏角色当前的攻击力 :" + this.vit + " 防御力 : " + this.def);
	}

	public int getVit() {
		return vit;
	}

	public void setVit(int vit) {
		this.vit = vit;
	}

	public int getDef() {
		return def;
	}

	public void setDef(int def) {
		this.def = def;
	}
	
	

}
package com.example.demo.memento.game;

public class Client {
	public static void main(String[] args) {
		//创建游戏角色
		GameRole gameRole = new GameRole(); 
		gameRole.setVit(100); 
		gameRole.setDef(100);
		System.out.println("和 boss 大战前的状态");
		gameRole.display();
		//把当前状态保存 caretaker
		Caretaker caretaker = new Caretaker(); 
		caretaker.setMemento(gameRole.createMemento());
		System.out.println("和 boss 大战~~~"); 
		gameRole.setDef(30); 
		gameRole.setVit(30);
		gameRole.display(); 
		System.out.println("大战后,使用备忘录对象恢复到站前");
		gameRole.recoverGameRoleFromMemento(caretaker.getMemento()); 
		System.out.println("恢复后的状态");
		gameRole.display();
	}
}

Notes and details of the memo mode

1) provides users with a mechanism to restore the state, allows users to easily return to the state by comparing a history.
2) to achieve a package of information, so that the user need not be concerned about the details of the state save.
3) If the member variable class too, is bound to take up a relatively large resource, and every time you save will consume a certain amount of memory, the need to pay attention.
4) the use of scenarios: 1, regret; 2, the archive when playing the game; 3, Windows in the ctri + z. 4, IE is retracted. 4, transaction management database.
5) In order to save memory, and a memo mode prototype patterns may accompany use.

Guess you like

Origin www.cnblogs.com/haizai/p/12622623.html