-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.DesignUndoORRedoFunctionality.java
More file actions
120 lines (93 loc) · 4.24 KB
/
42.DesignUndoORRedoFunctionality.java
File metadata and controls
120 lines (93 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* ------------------------------------------------------------
Undo / Redo Functionality (Java Single File)
Fully LLD-Compliant + Line-by-Line Explanation
------------------------------------------------------------- */
import java.util.*;
// ------------------------- DOCUMENT -------------------------
class Document {
String text; // holds the current text of document
Document() {
this.text = ""; // initialize with empty document
}
void show() {
System.out.println("Document Text: " + this.text); // print current state
}
}
// ----------------------- COMMAND PATTERN --------------------
interface Command { // base interface for all commands
void execute(); // perform the action
void unexecute(); // undo the action
}
// Concrete command: Add text to document
class AddTextCommand implements Command {
private Document doc; // reference to shared document
private String text; // text that will be added
AddTextCommand(Document doc, String text) {
this.doc = doc; // store document
this.text = text; // store content to add
}
@Override
public void execute() {
doc.text = doc.text + text; // append text to document
}
@Override
public void unexecute() {
doc.text = doc.text.substring(0, doc.text.length() - text.length());
// remove added text from end
}
}
// ----------------------- UNDO / REDO MANAGER -----------------
class UndoManager {
private Stack<Command> undoStack; // stores commands that CAN be undone
private Stack<Command> redoStack; // stores commands that CAN be redone
UndoManager() {
undoStack = new Stack<>(); // initialize stacks
redoStack = new Stack<>();
}
void execute(Command cmd) {
cmd.execute(); // perform the action
undoStack.push(cmd); // store it for undo
redoStack.clear(); // new action kills all redo history
}
void undo() {
if (undoStack.isEmpty()) { // no actions to undo
System.out.println("Nothing to undo.");
return;
}
Command cmd = undoStack.pop(); // pop last action
cmd.unexecute(); // revert it
redoStack.push(cmd); // push into redo history
}
void redo() {
if (redoStack.isEmpty()) { // no actions to redo
System.out.println("Nothing to redo.");
return;
}
Command cmd = redoStack.pop(); // take top undone command
cmd.execute(); // reapply it
undoStack.push(cmd); // push back to undo stack
}
}
// ----------------------------- DEMO ---------------------------
public class UndoRedoDemo {
public static void main(String[] args) {
Document doc = new Document(); // create shared document
UndoManager manager = new UndoManager();// create undo/redo manager
System.out.println("\n--- Performing Actions ---");
manager.execute(new AddTextCommand(doc, "Hello ")); // add "Hello "
manager.execute(new AddTextCommand(doc, "World")); // add "World"
doc.show(); // → Hello World
System.out.println("\n--- Undo 1 ---");
manager.undo(); // remove "World"
doc.show(); // → Hello
System.out.println("\n--- Undo 2 ---");
manager.undo(); // remove "Hello "
doc.show(); // → (empty)
System.out.println("\n--- Redo 1 ---");
manager.redo(); // redo "Hello "
doc.show(); // → Hello
System.out.println("\n--- Redo 2 ---");
manager.redo(); // redo "World"
doc.show(); // → Hello World
}
}