设计模式——备忘录模式

备忘录模式:在不破坏封装的前提下,存储关键对象的重要状态,从而可以在将来把对象还原到存储的那个状态
优点:
状态存储在外面,不和关键对象混在一起,这可以帮助维护内聚
提供了容易实现的恢复能力
保持了关键对象的数据封装
缺点:
资源消耗上面备忘录对象会很昂贵
存储和恢复状态的过程比较耗时

MementoIF .class

public interface public interface MementoIF {

}

MementoCaretaker.class

import java.util.HashMap;

public class MementoCaretaker {
    private HashMap mementomap;

    public MementoCaretaker() {
        mementomap = new HashMap();
    }

    public MementoIF retrieveMemento(String name) {
        return mementomap.get(name);
    }

    /**
     * 备忘录赋值方法
     */
    public void saveMemento(String name, MementoIF memento) {
        this.mementomap.put(name, memento);
    }
}

Originator.class


import java.util.HashMap;

public class Originator {
    private HashMap state;

    public Originator() {
        state = new HashMap();

    }

    public MementoIF createMemento() {
        return new Memento(state);
    }

    public void restoreMemento(MementoIF memento) {
        state = ((Memento) memento).getState();
    }

    public void showState() {
        System.out.println("now state:" + state.toString());
    }

    public void testState1() {
        state.put("blood", "500");
        state.put("progress", "gate1 end");
        state.put("enemy", "5");

    }

    public void testState2() {
        state.put("blood", "450");
        state.put("progress", "gate3 start");
        state.put("enemy", "3");

    }

    private class Memento implements MementoIF {

        private HashMap state;

        private Memento(HashMap state) {
            this.state = new HashMap(state);
        }

        private HashMap getState() {
            return state;
        }

        private void setState(HashMap state) {
            this.state = state;
        }
    }
}

Originator2.class

import java.util.ArrayList;

public class Originator2 {
    private ArrayList state;

    public Originator2() {
        state = new ArrayList();
    }

    public MementoIF createMemento() {
        return new Memento(state);
    }

    public void restoreMemento(MementoIF memento) {
        state = ((Memento) memento).getState();
    }

    public void testState1() {
        state = new ArrayList();
        state.add("blood:320");
        state.add("progress:gate2 mid");
        state.add("enemy:15");

    }

    public void testState2() {
        state = new ArrayList();
        state.add("blood:230");
        state.add("progress:gate8 last");
        state.add("enemy:12");

    }

    public void showState() {
        System.out.println("now state:" + state.toString());
    }

    private class Memento implements MementoIF {

        private ArrayList state;

        private Memento(ArrayList state) {
            this.state = new ArrayList(state);
        }

        private ArrayList getState() {
            return state;
        }

        private void setState(ArrayList state) {
            this.state = state;
        }
    }

}

MainTest.class

public class MainTest {

    public static void main(String[] args) {
        MementoCaretaker mMementoCaretaker = new MementoCaretaker();
        Originator mOriginator = new Originator();
        Originator2 mOriginator2 = new Originator2();

        System.out.println("*****Originator*****");
        mOriginator.testState1();
        mMementoCaretaker
                .saveMemento("Originator", mOriginator.createMemento());
        mOriginator.showState();
        mOriginator.testState2();
        mOriginator.showState();
        mOriginator.restoreMemento(mMementoCaretaker
                .retrieveMemento("Originator"));
        mOriginator.showState();

        System.out.println("*****Originator 2*****");
        mOriginator2.testState1();
        mOriginator2.showState();
        mMementoCaretaker.saveMemento("Originator2",
                mOriginator2.createMemento());
        mOriginator2.testState2();
        mOriginator2.showState();
        mOriginator2.restoreMemento(mMementoCaretaker
                .retrieveMemento("Originator2"));
        mOriginator2.showState();

        //System.out.println("*****Originator&&Originator 2*****");
    //   mOriginator.restoreMemento(mMementoCaretaker
    //   .retrieveMemento("Originator2"));
    //   mOriginator.showState();

    }

}

你可能感兴趣的:(android,设计模式)