No Undo/Redo History
Descriptionβ
What does it look like?β
- User actions are not recorded, so undo/redo functionality is unavailable
- There is no way to revert the side effects of an operation
- Without a history, it is difficult to restore or roll back system state
Why is it a problem?β
- Poor user experience (UX) β no way to undo mistakes
- Recovery from input errors requires redoing or restoring manually
- Difficult to debug or trace issues in complex state changes
Bad Example of the Anti-patternβ
- TypeScript
- PHP
- Python
class TextEditor {
private content: string = "";
type(text: string) {
this.content += text;
}
getContent() {
return this.content;
}
}
const editor = new TextEditor();
editor.type("Hello ");
editor.type("World");
console.log(editor.getContent()); // Hello World
// εγζΆγγγ§γγͺγοΌ
<?php
class TextEditor {
private string $content = "";
public function type(string $text): void {
$this->content .= $text;
}
public function getContent(): string {
return $this->content;
}
}
// ε©η¨δΎ
$editor = new TextEditor();
$editor->type("Hello ");
$editor->type("World");
echo $editor->getContent(); // Hello World
// εγζΆγγγ§γγͺγοΌ
class TextEditor:
def __init__(self):
self.content = ""
def type(self, text: str):
self.content += text
def get_content(self) -> str:
return self.content
# ε©η¨δΎ
editor = TextEditor()
editor.type("Hello ")
editor.type("World")
print(editor.get_content()) # Hello World
# εγζΆγγγ§γγͺγοΌ
Issues:β
- Input is applied directly to the internal state β no way to undo
type()
modifies the content immediately, making history management difficult
Refactoring by Patternβ
Design patterns that can address thisβ
Pattern | Overview | Main Refactoring Approach |
---|---|---|
Command | Encapsulate operations as command objects and manage execute/undo/redo | Store and control execution history |
Memento | Capture and restore full snapshots of an objectβs state | Enable complete state restoration |