Undo/Redo implementation

后端 未结 10 1345
醉酒成梦
醉酒成梦 2020-11-28 18:59

Give me some thoughts how to implement undo/redo functionality - like we have in text editors. What algorithms should I use and what I may read. thanks.

相关标签:
10条回答
  • 2020-11-28 19:20

    Adding to the discussion, I wrote a blog post on how to implement the UNDO and REDO based on thinking about what is intuitive: http://adamkulidjian.com/undo-and-redo.html

    0 讨论(0)
  • 2020-11-28 19:21

    There are several ways to do this, but you could start looking at the Command pattern. Use a list of commands to move back (Undo) or forward (redo) through your actions. An example in C# can be found here.

    0 讨论(0)
  • 2020-11-28 19:28

    The Memento pattern was made for this.

    Before implementing this yourself, note that this is quite common, and code already exist - For example, if you're coding in .Net, you can use IEditableObject.

    0 讨论(0)
  • 2020-11-28 19:31

    If the actions are reversible. e.g Adding 1, make a player move etc see how to use the Command Pattern to implement undo/redo. Follow the link the you will find a detailed examples on how to do that.

    If not, use Saved State as explained by @Lazer.

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