How do I implement a simple undo/redo for actions in java?

前端 未结 5 843
旧时难觅i
旧时难觅i 2020-12-15 07:17

I\'ve created an XML editor and I\'m stuck at the last phase: adding undo/redo functionality.

I\'ve only got to add undo/redo for when users add elements, attributes

5条回答
  •  萌比男神i
    2020-12-15 07:37

    You have to define undo(), redo() operations along with execute() in Command interface itself.

    example:

    interface Command {
    
        void execute() ;
    
        void undo() ;
    
        void redo() ;
    }
    

    Define a State in your ConcreteCommand class. Depending on current State after execute() method, you have to decide whether command should be added to Undo Stack or Redo Stack and take decision accordingly.

    Have a look at this undo-redo command article for better understanding.

提交回复
热议问题