Brief introduction
How do we use these methods to implement Undo/redo in different scenarios? These methods use a single object to represent change, command mode, and Memo mode.
As we know, Undo/redo does not have a common solution, and Undo/redo is very specific in every application. For this reason, at the beginning of the series, we will discuss how to use this method to model any application, and then show the implementation of a simple application.
On the basic idea of Undo/redo realization
As we know, the application changes its state after each operation. When you manipulate an application, its state changes. So, if someone wants to undo it, he has to go back to the previous state. Therefore, in order to be able to go back to the previous state, we need to store its state while the application is running. To support redo, we have to jump from the current state to the next state.
To implement Undo/redo, we had to store the state of the application and skip to the previous state at the time of the undo and skip to the next state when redo. So we need to maintain the state of the application to support Undo/redo. Of all three methods, the maintenance of application state is used for two stacks. A stack contains the state for the undo operation, and the second contains the state for the redo. The Undo action pops up the undo stack to get the previous state and set it to the application. Similarly, the redo operation pops up the redo stack to get the next state and set it to the application.
Now, we know that the Undo/redo implementation is all about keeping the application state after each operation. The question now is how the method saves state. In this method, the change of a single operation is saved in an object, and some attributes are superfluous for the operation as a state, where a single object is used to contain all types of action data.
What is a single object representing the method of change?
First of all, I am sorry that it was named by me. Here, a single object represents all the changes in all operations in the application. So, when you prepare an object about the type of operation Change, after performing an operation, you use only a subset of the object's properties, and the remaining attributes are still not in use. For example, you have two operations in an application; they are changes in height and width. Therefore, the object type contains two properties: Height and width. When you have a change object in place, you only need to set the height field of the variable object, and the other fields are still not in use after you perform the height change method.
How to apply a single object representation change method to model any application Undo/redo operations?
How a single object represents a changing method modeling any application Undo/redo action is discussed in the following steps:
Step 1
First identify which operations you want to support Undo/redo. Then, identify which container you will support Undo/redo and which objects you want to support Undo/redo.
Step 2
To further process each Undo/redo operation, identify the attributes that need to be saved.
Step 3
It then creates a class (Changerepresentationobject) that contains all the properties that support all operations Undo/redo. Also, prepare an enum for the action type, which will represent all actions. This action type Enum is part of the Changerepresentationobject class.