1、例子
- public class Caretaker {
-
- private Memento memento;
-
- private ArrayList<Memento> mementoArrayList;
-
- private HashMap<String,ArrayList<Memento>> map;
-
- public Memento getMemento() {
- return memento;
- }
-
- public void setMemento(Memento memento) {
- this.memento = memento;
- }
- }
- public class GameRole {
-
- private int vit;
- private int def;
-
- public Memento createMemento(){
- return new Memento(vit,def);
- }
-
- public void recoverGameRoleFromMemento(Memento memento){
- this.vit=memento.getVit();
- this.def=memento.getDef();
- }
-
- public void display(){
- System.out.println("攻击力"+this.def+"防御力"+this.vit);
- }
-
- 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;
- }
- }
- public class Memento {
-
- private int vit;
- private int def;
-
- public Memento(int vit, int def) {
- 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;
- }
- }
- public class Client {
- public static void main(String[] args) {
- GameRole gameRole = new GameRole();
- gameRole.setDef(100);
- gameRole.setVit(100);
-
- gameRole.display();
-
- Caretaker caretaker=new Caretaker();
- caretaker.setMemento(gameRole.createMemento());
-
-
- gameRole.setVit(30);
- gameRole.setDef(30);
-
- gameRole.display();
-
-
- gameRole.recoverGameRoleFromMemento(caretaker.getMemento());
-
-
- gameRole.display();
-
-
- }
- }