package com.maom.design.D_22_Memento;
public class D_22_Memento {
public static void main(String[] args) {
Person per = new Person("lifengxing", 24);
Caretaker caretaker = new Caretaker();
caretaker.setMemento(per.createMemento());
per.display();
per.setName("beifeng");
per.setAge(1);
per.display();
per.setMemento(caretaker.getMemento());
per.display();
}
}
class Person {
private String name;
private int age;
public Person() {}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void display() {
System.out.println("name:" + name + ",age:" + age);
}
public Memento createMemento() {
return new Memento(name, age);
}
public void setMemento(Memento memento) {
this.name = memento.getName();
this.age = memento.getAge();
}
}
class Memento {
private String name;
private int age;
public Memento(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Caretaker {
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}