欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

设计模式之备忘录(memento)

程序员文章站 2022-05-03 21:39:50
...

 

public class Memento {
	public static void main(String[] args){
		AnOriginator ao = new AnOriginator();
		ao.setA("5");
		ao.setB("c");
		AMemento am = ao.getAMemento();
		ao.setA("3");
		ao.setB("i");
		System.out.println(ao.getA()+ao.getB());
		System.out.println(am.getA()+am.getB());
	}
	
}

class AnOriginator{
	private String a;
	private String b;
	
	public AMemento getAMemento(){
		return new AMemento(this);
	}
	public String getA() {
		return a;
	}
	public void setA(String a) {
		this.a = a;
	}
	public String getB() {
		return b;
	}
	public void setB(String b) {
		this.b = b;
	}
	
}

class AMemento{
	private String a;
	private String b;
	public AMemento(AnOriginator anOriginator){
		this.a = anOriginator.getA();
		this.b = anOriginator.getB();
	}
	public String getA() {
		return a;
	}
	public void setA(String a) {
		this.a = a;
	}
	public String getB() {
		return b;
	}
	public void setB(String b) {
		this.b = b;
	}
}
//http://zuoqiang.iteye.com/blog/744024