Home  >  Article  >  Java  >  Sample code for Java Memento pattern implementation

Sample code for Java Memento pattern implementation

黄舟
黄舟Original
2017-03-11 11:26:201476browse

Sample code for Java Memento pattern implementation

/**
 * 数据对象
 * @author stone
 *
 */
public class DataState {
	private String action;

	public void setAction(String action) {
		this.action = action;
	}
	
	public String getAction() {
		return action;
	}

}
/**
 * 一个保存另外一个对象内部状态拷贝 的对象.这样以后就可以将该对象恢复到原先保存的状态.
 * 
 * @author stone
 *
 */
import java.io.File;
import java.io.Serializable;

public class Memento implements Serializable {

	/*private int number;
	private File file = null;

	public Memento(Originator o) {
		this.number = o.getNumber();
		this.file = o.getFile();
	}

	public int getNumber() {
		return this.number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public File getFile() {
		return this.file;
	}

	public void setFile(File file) {
		this.file = file;
	}
*/
	private DataState state;
	public Memento(Originator o) {
		this.state = o.getState();
	}
	
	public DataState getState() {
		return state;
	}
	
	public void setState(DataState state) {
		this.state = state;
	}
}
public class Originator {

/*	private int number;
	private File file = null;

	public Originator() {

	}

	// 创建一个Memento,将自身作为参数传入
	public Memento getMemento() {
		return new Memento(this);
	}

	// 从Memento中取出保存的数据,恢复为原始状态
	public void setMemento(Memento m) {
		number = m.getNumber();
		file = m.getFile();
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}*/
	
	private DataState state;
	public Originator() {
		
	}
	
	public Originator(DataState state) {
		this.state = state;
	}
	
	// 创建一个Memento,将自身作为参数传入
	public Memento getMemento() {
		return new Memento(this);
	}
	
	// 从Memento中取出保存的数据,恢复为原始状态
	public void setMemento(Memento m) {
		/*
		 * getMemento() 创建的对象,保存在某个容器里,
		 * 当需要恢复时,将其传入当前方法, 再使用getState(),得出
		 */
		this.state = m.getState();
	}
	
	public DataState getState() {
		return state;
	}
	
	public void setState(DataState state) {
		this.state = state;
	}
}
/*
 * 备忘录(Memento)模式 行为型模式
 * 流程: Memento用于保存 数据状态,
 * 		Originator用于 加载数据, 建立Memento对象,及通过Memento恢复原始数据
 */
public class Test {
	public static void main(String[] args) {
		
//		Originator originator = new Originator();
//		originator.setNumber(8);
//		
//		Memento memento = originator.getMemento();
//		System.out.println(memento.getNumber());
		
		DataState state = new DataState();
		state.setAction("copy a character");
		Originator originator = new Originator();
		System.out.println("创建原始数据");
		originator.setState(state);
		
		System.out.println("创建备忘录对象, 保存原始数据状态");
		Memento memento = originator.getMemento();
		
		System.out.println("创建了一个新数据");
		originator.setState(new DataState());

		System.out.println("创建新数据后:" + originator.getState().getAction());
		
		/*
		 * memento 需要保存在某地,需要时取出,以恢复它内部所保存的数据
		 */
		System.out.println("创建新数据后,恢复原数据");
		originator.setMemento(memento);
		System.out.println(originator.getState().getAction());
	}
}

Print

创建原始数据
创建备忘录对象, 保存原始数据状态
创建了一个新数据
创建新数据后:null
创建新数据后,恢复原数据
copy a character

The above is the detailed content of Sample code for Java Memento pattern implementation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn