设计模式---备忘录模式

以前


public class MainClass {
	public static void main(String[] args) {
		Person per = new Person("lifengxing","男",30);
		
		//保存内部状态
		Person backup = new Person();
		backup.setName(per.getName());
		backup.setAge(per.getAge());
		backup.setSex(per.getSex());
		
		per.display();
		
		//修改
		per.setAge(20);
		per.display();
		
		//回滚 还原
		per.setName(backup.getName());
		per.setSex(backup.getSex());
		per.setAge(backup.getAge());
		
		per.display();
		
	}
}

 


public class Person {
	//姓名
	private String name;
	//性别
	private String sex;
	//年龄
	private int age;
	
	public Person() {
		
	}
	
	public Person(String name, String sex, int age) {
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public void display() {
		System.out.println("name:" + name + ",sex:" + sex + ",age:" + age);
	}
	
}

备忘录模式

package com.ibeifeng.ex2;

public class Person {
	//姓名
	private String name;
	//性别
	private String sex;
	//年龄
	private int age;
	
	public Person() {
		
	}
	
	public Person(String name, String sex, int age) {
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public void display() {
		System.out.println("name:" + name + ",sex:" + sex + ",age:" + age);
	}
	
	//创建一个备份
	public Memento createMemento() {
		return new Memento(name,sex,age);
	}
	
	//恢复备份,还原
	public void setMemento(Memento memento) {
		this.name = memento.getName();
		this.sex = memento.getSex();
		this.age =  memento.getAge();
	}
	
}

 

package com.ibeifeng.ex2;

public class Memento {
	// 姓名
	private String name;
	// 性别
	private String sex;
	// 年龄
	private int age;
	
	public Memento(String name, String sex, int age) {
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}
package com.ibeifeng.ex2;

public class Caretaker {
	private Memento memento;

	public Memento getMemento() {
		return memento;
	}

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

 

package com.ibeifeng.ex2;

public class MainClass {
	public static void main(String[] args) {
		Person per = new Person("lifengxing","男",24);
		
//		Memento memento = per.createMemento();
		Caretaker caretaker = new Caretaker();
		caretaker.setMemento(per.createMemento());
		
		per.display();
		
		per.setName("beifeng");
		per.setSex("女");
		per.setAge(1);
		
		per.display();
		
		per.setMemento(caretaker.getMemento());
		per.display();
		
	}
}