使用備忘錄模式實現Undo和Redo

備忘錄模式有三個角色。
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<Memento> 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章