Java - Memento模式和撤消 [英] Java - Memento pattern and Undo

查看:177
本文介绍了Java - Memento模式和撤消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个撤消/重做功能,它要求我使用memento模式。

I am implementing an undo/redo function which requires me to use memento pattern.

部分程序的流程:...程序然后存储以前使用Memento Pattern的Vector,然后将新创建的对象添加到Vector中。之后,用户可以选择show命令来显示Vector内部的内容,也可以输入undo命令进行恢复,撤消可以重复直到它恢复到原始状态......

The flow of the partial program : "...the program then store the previous Vector using Memento Pattern, then the newly created object will be added to the Vector. After that, user may choose a show command to show what is inside the Vector, he can also enter undo command to restore, the undo can be repeated until it is restored to the original state..."

从我的研究中,我知道会有一个创始人,纪念品和看护人。

From my research, I know there will be an originator, memento and caretaker.

这是我的看守程序

public class CareTaker {
      private Memento m;
      private Stack s;
      private Vector v;
      // Some of the implementation are not shown

      public void create() {
            // Some of the implementation are not shown
            // Assuming Vector is named "v"
            // Passing Vector to memento
            m = new Memento(v);
            s.add(m);
      }
      public void undo() {
          v = s.pop().restore();
      }
}
public class Memento {
    private Vector _v;
    public Memento(Vector v) {
      _v = v;
    }
    public Vector restore() {
      return _v;
    }
}

不幸的是,我没有确定发起人也没有我知道会是哪一个。
如果没有Originator,这个代码片段是否是正确的Memento模式?

Unfortunately , I failed to identify the "Originator" nor I know which one will be. Is this code fragment ever a correct Memento pattern if there is no Originator?

推荐答案

memento模式用于保存对象的状态而不知道它的内部数据结构。

The memento pattern is used to save the state of an object without knowing it's internal data structures.

我尝试用 Iterator 示例解释它

public class MementoListIterator<E> implements Iterator<E> {

    public static class Memento {

        private int savedIndex;

        private Memento(MementoListIterator<?> mementoListIterator) {
            this.savedIndex = mementoListIterator.index;
        }

    }

    private List<E> elements;

    private int index = 0;

    public MementoListIterator(List<E> elements) {
        this.elements = elements;
    }

    public Memento save() {
        return new Memento(this);

    }

    public void restore(Memento memento) {
        this.index = memento.savedIndex;
    }

    @Override
    public boolean hasNext() {
        return this.index < elements.size();
    }

    @Override
    public E next() {
        return elements.get(index++);
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Not implemented yet");
    }
}

客户端现在可以保存迭代器的任何状态而不用知道迭代器如何在内部管理它的状态。

A client can now save any state of the iterator without knowing how the iterator internally manages it's state.

public class Main {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("A", "B", "C", "D", "E");
        MementoListIterator<String> mementoListIterator = new MementoListIterator<String>(
                list);

        Memento initialState = mementoListIterator.save();

        while (mementoListIterator.hasNext()) {
            String string = mementoListIterator.next();
            System.out.println(string);
        }
                    // Normally we can not re-use the iterator, but
                    // fortuanatly we saved the initial state.

        // restore the initial state and we can use the Iterator again
        mementoListIterator.restore(initialState);

        while (mementoListIterator.hasNext()) {
            String string = mementoListIterator.next();
            System.out.println(string);
        }
    }
}

这篇关于Java - Memento模式和撤消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆