Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-Memento模式和撤销_Java_Design Patterns_Memento - Fatal编程技术网

Java-Memento模式和撤销

Java-Memento模式和撤销,java,design-patterns,memento,Java,Design Patterns,Memento,我正在实现一个撤销/重做功能,它要求我使用memento模式 部分程序的流程:“…然后程序使用Memento模式存储上一个向量,然后新创建的对象将添加到向量中。之后,用户可以选择一个显示命令来显示向量内部的内容,他也可以输入undo命令来恢复,可以重复撤销,直到恢复到原始状态……” 从我的研究中,我知道会有一个发起者、纪念品和看护人 这是我的管理员程序 public class CareTaker { private Memento m; private Stack s;

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

部分程序的流程:“…然后程序使用Memento模式存储上一个向量,然后新创建的对象将添加到向量中。之后,用户可以选择一个显示命令来显示向量内部的内容,他也可以输入undo命令来恢复,可以重复撤销,直到恢复到原始状态……”

从我的研究中,我知道会有一个发起者、纪念品和看护人

这是我的管理员程序

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;
    }
}
不幸的是,我无法确定“发起人”,也不知道哪一个将是。
如果没有发起者,此代码片段是否是正确的Memento模式?

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

我试图用一个
迭代器
示例来解释它

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");
    }
}
公共类MementoListIterator实现迭代器{
公共静态类纪念品{
私有int-savedIndex;
私人纪念品(MementoListIterator MementoListIterator){
this.savedIndex=mementoListIterator.index;
}
}
私有列表元素;
私有整数指数=0;
公共MementoListIterator(列表元素){
这个元素=元素;
}
公共纪念品保存(){
归还新的纪念品(本);
}
公共无效恢复(Memento Memento){
this.index=memento.savedIndex;
}
@凌驾
公共布尔hasNext(){
返回this.index
客户端现在可以保存迭代器的任何状态,而不知道迭代器如何在内部管理其状态

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);
        }
    }
}
公共类主{
公共静态void main(字符串[]args){
列表=数组。asList(“A”、“B”、“C”、“D”、“E”);
MementoListIterator MementoListIterator=新MementoListIterator(
名单);
Memento initialState=mementoListIterator.save();
while(mementoListIterator.hasNext()){
String=mementoListIterator.next();
System.out.println(字符串);
}
//通常我们不能重复使用迭代器,但是
//第四,我们保存了初始状态。
//恢复初始状态,我们可以再次使用迭代器
mementoListIterator.restore(initialState);
while(mementoListIterator.hasNext()){
String=mementoListIterator.next();
System.out.println(字符串);
}
}
}