メモモードを使用してUndoとRedoを実現


メモモードには3つの役割があります.1.イニシエータ(Originator)ロール:現在の時刻自体の内部状態を記録するメモを作成し、メモを使用して内部状態を復元できます.その他のビジネス機能を実現します.2.メモ(Memento)ロール:イニシエータの内部ステータスを格納します.3.管理者(Caretaker)ロール:メモを管理し、メモを保存および取得する機能を提供しますが、メモの内容にアクセスおよび変更することはできません.
/**
 *      
 *
 */
public class UnRedoOriginator {
    private String state;

    public UnRedoOriginator() {
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }

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

    public void restoreMemento(Memento m) {
        if (m != null) {
            this.setState(m.getState());
        } else {
            System.out.println("       ");
        }
    }
}
/**
 *    
 *
 */
public class Memento {
    private String state;

    public Memento(String state) {
        this.state = state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }
}
/**
 *  undo redo       
 *
 */
public class UnRedoCaretaker {
    /*      */
    private List mementoList;
    /*       ,       =        + 1 */
    private int capacity = 3;
    /*      */
    private int undoIndex = -2;
    /*      */
    private int redoIndex = 0;

    public UnRedoCaretaker() {
        this.mementoList = new LinkedList<>();
    }

    public UnRedoCaretaker(int capacity) {
        this();
        this.capacity = capacity;
    }

    /**
     *     
     * 
     * @param memento
     */
    public void addMemento(Memento memento) {
        //      ,           
        for (int i = this.mementoList.size() - 1; i > this.undoIndex + 1; i--) {
            this.mementoList.remove(i);
        }

        if (this.mementoList.size() >= this.capacity) {
            this.mementoList.remove(0);
        }
        this.mementoList.add(memento);

        this.undoIndex = this.mementoList.size() - 2;
        this.redoIndex = this.mementoList.size();
    }

    /**
     *     
     * 
     * @return
     */
    public Memento undo() {
        Memento result = null;

        if (this.undoIndex >= 0 && this.undoIndex < this.mementoList.size() - 1) {
            result = this.mementoList.get(this.undoIndex);

            this.undoIndex--;
            this.redoIndex--;
        }

        return result;
    }

    /**
     *     
     * 
     * @return
     */
    public Memento redo() {
        Memento result = null;
        if (this.redoIndex > 0 && this.redoIndex < this.mementoList.size()) {
            result = this.mementoList.get(this.redoIndex);

            this.redoIndex++;
            this.undoIndex++;
        }

        return result;
    }
}

イニシエータロールとメモ管理者ロールを集約し、イニシエータロールクラスを変更します.
/**
 *      
 *
 */
public class UnRedoOriginator {
    private String state;
    private UnRedoCaretaker caretaker;

    public UnRedoOriginator() {
        this.caretaker = new UnRedoCaretaker(3);
    }

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

    public String getState() {
        return state;
    }

    private Memento createMemento() {
        return new Memento(state);
    }

    private void restoreMemento(Memento m) {
        if (m != null) {
            this.setState(m.getState());
        } else {
            System.out.println("       ");
        }
    }

    public void setAndStoreState(String state) {
        this.setState(state);
        caretaker.addMemento(this.createMemento());
    }

    public void undo() {
        this.restoreMemento(caretaker.undo());
    }

    public void redo() {
        this.restoreMemento(caretaker.redo());
    }
}

テスト.
/**
 *    undo redo
 * 
 */
public class TestUnRedo {

    public static void main(String[] args) {
        UnRedoOriginator or = new UnRedoOriginator();

        /*           */
        or.undo();
        or.redo();

        /*        */
        System.out.println();
        int num = 1;
        operation(or, num++);
        operation(or, num++);
        operation(or, num++);
        operation(or, num++);

        /*             */
        back(or);
        back(or);
        back(or);

        /*         ,      */
        System.out.println();
        operation(or, num++);
        back(or);
        forward(or);
        forward(or);//            

        /*         ,  redo */
        System.out.println();
        back(or);
        operation(or, num++);
        forward(or);
    }

    private static void operation(UnRedoOriginator or, int name) {
        System.out.println("*******  *******");
        or.setAndStoreState("  " + name);
        System.out.println("    :" + or.getState());
    }

    private static void back(UnRedoOriginator or) {
        System.out.println("-------  -------");
        or.undo();
        System.out.println("    :" + or.getState());
    }

    private static void forward(UnRedoOriginator or) {
        System.out.println("=======  =======");
        or.redo();
        System.out.println("    :" + or.getState());
    }

}

運転結果は以下の通りです.
       
       

*******  *******
    :  1
*******  *******
    :  2
*******  *******
    :  3
*******  *******
    :  4
-------  -------
    :  3
-------  -------
    :  2
-------  -------
       
    :  2

*******  *******
    :  5
-------  -------
    :  2
=======  =======
    :  5
=======  =======
       
    :  5

-------  -------
    :  2
*******  *******
    :  6
=======  =======
       
    :  6