There is a collection in which the user can add objects and delete them.
Tell me how you can implement the functionality of undo / redo changes using Ctrl + Z / Ctrl + Y?
I would be grateful for any suggestions and links.
The classic solution to this problem is to use the command pattern.
Suppose there is a code in the Execute() method that performs some useful action, and in the Undo() method there is a code that cancels this change (in your case, the collection changes are rolled back). You need a stack in which you will place such objects before calling the Execute() method on them and, accordingly, by Ctrl + Z you will unwind such a stack, causing the Undo() method of the operation to be rolled back
public class Command { public void Execute() { // ΠΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ } public void Undo() { // ΠΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΎΡΠΊΠ°ΡΠ° ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ } } Source: https://ru.stackoverflow.com/questions/632715/
All Articles