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

前端 未结 5 834
旧时难觅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条回答
  • 2020-12-15 07:30

    This tutorial explains the fundamental of the Command Pattern and the undo/redo mechanism for Swing. Hope it helps.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 07:45

    TL;DR: You can support undo and redo actions by implementing the Command and Memento patterns (Design Patterns - Gama et. al).

    The Memento Pattern

    This simple pattern allows you to save the states of an object. Simply wrap the object in a new class and whenever its state changes, update it.

    public class Memento
    {
        MyObject myObject;
    
        public MyObject getState()
        {
            return myObject;
        }
    
        public void setState(MyObject myObject)
        {
            this.myObject = myObject;
        }
    }
    

    The Command Pattern

    The Command pattern stores the original object (that we want to support undo/redo) and the memento object, which we need in case of an undo. Moreover, 2 methods are defined:

    1. execute: executes the command
    2. unExecute: removes the command

    Code:

    public abstract class Command
    {
        MyObject myObject;
        Memento memento;
    
        public abstract void execute();
    
        public abstract void unExecute();
    }
    

    The define the logical "Actions" that extend Command (e.g. Insert):

    public class InsertCharacterCommand extends Command
    {
        //members..
    
        public InsertCharacterCommand()
        {
            //instantiate 
        }
    
        @Override public void execute()
        {
            //create Memento before executing
            //set new state
        }
    
        @Override public void unExecute()
        {
            this.myObject = memento.getState()l
        }
    }
    

    Applying the patterns:

    This last step defines the undo/redo behaviour. They core idea is to store a stack of commands that works as a history-list of the commands. To support redo, you can keep a secondary pointer whenever an undo command is applied. Note that whenever a new object is inserted, then all the commands after its current position get removed; that's achieved by the deleteElementsAfterPointer method defined below:

    private int undoRedoPointer = -1;
    private Stack<Command> commandStack = new Stack<>();
    
    private void insertCommand()
    {
        deleteElementsAfterPointer(undoRedoPointer);
        Command command =
                new InsertCharacterCommand();
        command.execute();
        commandStack.push(command);
        undoRedoPointer++;
    }
    
    private void deleteElementsAfterPointer(int undoRedoPointer)
    {
        if(commandStack.size()<1)return;
        for(int i = commandStack.size()-1; i > undoRedoPointer; i--)
        {
            commandStack.remove(i);
        }
    }
    
     private void undo()
    {
        Command command = commandStack.get(undoRedoPointer);
        command.unExecute();
        undoRedoPointer--;
    }
    
    private void redo()
    {
        if(undoRedoPointer == commandStack.size() - 1)
            return;
        undoRedoPointer++;
        Command command = commandStack.get(undoRedoPointer);
        command.execute();
    }
    

    Conclusion:

    What makes this design powerful is the fact that you can add as many commands as you like (by extending the Command class) e.g., RemoveCommand, UpdateCommand and so on. Moreover, the same pattern is applicable to any type of object, making the design reusable and modifiable across different use cases.

    0 讨论(0)
  • 2020-12-15 07:47

    Take a look at the Command Pattern, its uses include implementing undo/redo functionality.

    0 讨论(0)
  • 2020-12-15 07:51

    I would try to create an Action class, with a AddElementAction class inheriting off Action. AddElementAction could have a Do() and Undo() method which would add/remove elements accordingly. You can then keep two stacks of Actions for undo/redo, and just call Do()/Undo() on the top element before popping it.

    0 讨论(0)
提交回复
热议问题