Implementation of undo and redo in Java

I want to ask a question about List in Java.

It is easy to implement the removal, addition and search of an item in a list. But how to implement cancellation and list replay in Java?

Can someone help me with this?

+3
source share
4 answers

You might want to implement a Command Design Pattern for this. A nice simplified example for List can be found here http://www.algosome.com/articles/implementing-undo-redo-java.html

+4
source

I think you want to cancel and redo operations such as deleting and adding to the list.

Just use a different list with indexing capabilities for Undo and Redo , for example. ArrayList:

  • Each time an add element (element) or delete (element) in the source list really changes this list, you put the element at the end of your cancellation list.
  • Then, when you want to cancel the operation, you simply move around the undo list: If the item in the undo list is not in the original list, add it, if it is in the original list, delete it,
  • If you want to use your cancellation list for redo, also do not delete the items that you just โ€œunzippedโ€ from the cancellation list, but rather go through the cancellation list through the index. You can then move around the cancellation list in both directions and, therefore, cancel and repeat the operation.
+1
source

Do you mean how to remove an item from the list and cancel it? You can easily create a new class for the list and define properties such as: the last action performed + save the value of the beginning (and, possibly, index) of the processed element. The same goes for repeat (at least for one repeat and cancel step). If you donโ€™t care about the order of the items (or you can easily order them), specify a list of the last actions performed and a list of values โ€‹โ€‹of the initial value. So, for example: lastAction [0] = "Delete"; lastElement [0] = 1; // means that you removed 1 from the list

This is the first and fictitious idea of โ€‹โ€‹how to do this. There may be some problems to consider ...

0
source

This requires a binding.

For me, this is the most effective way to do this. This question at SO can give you some tips on where to start.

0
source

Source: https://habr.com/ru/post/985218/


All Articles