What is Command design pattern?
javascript
class Command {
execute() {}
undo() {}
}
class AddTextCommand extends Command {
constructor(editor, text) {
super();
this.editor = editor;
this.text = text;
}
execute() {
this.editor.add(this.text);
}
undo() {
this.editor.remove(this.text);
}
}
class Editor {
constructor() {
this.history = [];
}
executeCommand(command) {
command.execute();
this.history.push(command);
}
undo() {
const command = this.history.pop();
command?.undo();
}
}Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.