【问题标题】:Java - Memento pattern and UndoJava - 备忘录模式和撤消
【发布时间】:2013-12-20 20:26:15
【问题描述】:

我正在实现一个需要我使用备忘录模式的撤消/重做功能。

部分程序的流程:“......程序然后使用Memento Pattern存储之前的Vector,然后将新创建的对象添加到Vector中。之后,用户可以选择一个show命令来显示什么是在Vector里面,他也可以输入undo命令来恢复,可以重复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;
    }
}

很遗憾,我未能确定“发起人”,也不知道会是谁。 如果没有 Originator,这个代码片段是否曾经是正确的 Memento 模式?

【问题讨论】:

    标签: java design-patterns memento


    【解决方案1】:

    备忘录模式用于在不知道对象内部数据结构的情况下保存对象的状态。

    我尝试用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");
        }
    }
    

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

    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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-28
      • 2011-03-16
      • 1970-01-01
      • 2013-12-01
      • 2012-07-09
      • 2020-07-05
      • 2013-08-27
      • 1970-01-01
      • 2021-01-22
      相关资源
      最近更新 更多