요청을 요청에 대한 모든 정보가 포함된 독립실행형 객체로 변환하는 행동 디자인 패턴

abstract class Command {
protected Application app;
protected Editor editor;
private String backup;
public Command(Application app, Editor editor) {
this.app = app;
this.editor = editor;
}
public void saveBackup() {
backup = editor.getText();
}
public void undo() {
editor.setText(backup);
}
public abstract boolean execute();
}
class CopyCommand extends Command {
public CopyCommand(Application app, Editor editor) {
super(app, editor);
}
@Override
public boolean execute() {
app.setClipboard(editor.getSelection());
return false;
}
}
class CutCommand extends Command {
public CutCommand(Application app, Editor editor) {
super(app, editor);
}
@Override
public boolean execute() {
saveBackup();
app.setClipboard(editor.getSelection());
editor.deleteSelection();
return true;
}
}
class PasteCommand extends Command {
public PasteCommand(Application app, Editor editor) {
super(app, editor);
}
@Override
public boolean execute() {
saveBackup();
editor.replaceSelection(app.getClipboard());
return true;
}
}
class UndoCommand extends Command {
public UndoCommand(Application app, Editor editor) {
super(app, editor);
}
@Override
public boolean execute() {
app.undo();
return false;
}
}
class CommandHistory {
private java.util.Stack<Command> history = new java.util.Stack<>();
public void push(Command command) {
history.push(command);
}
public Command pop() {
return history.isEmpty() ? null : history.pop();
}
}
class Editor {
private String text = "";
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getSelection() {
return "selected text";
}
public void deleteSelection() {
System.out.println("Selection deleted.");
}
public void replaceSelection(String text) {
System.out.println("Replaced with: " + text);
}
}
class Application {
private String clipboard;
private Editor activeEditor;
private CommandHistory history;
public Application() {
this.history = new CommandHistory();
}
public void setClipboard(String clipboard) {
this.clipboard = clipboard;
}
public String getClipboard() {
return clipboard;
}
public void setActiveEditor(Editor editor) {
this.activeEditor = editor;
}
public void createUI() {
Command copy = new CopyCommand(this, activeEditor);
Command cut = new CutCommand(this, activeEditor);
Command paste = new PasteCommand(this, activeEditor);
Command undo = new UndoCommand(this, activeEditor);
executeCommand(copy);
executeCommand(cut);
executeCommand(paste);
executeCommand(undo);
}
public void executeCommand(Command command) {
if (command.execute()) {
history.push(command);
}
}
public void undo() {
Command command = history.pop();
if (command != null) {
command.undo();
}
}
public static void main(String[] args) {
Application app = new Application();
Editor editor = new Editor();
app.setActiveEditor(editor);
app.createUI();
app.executeCommand(new CopyCommand(app, editor));
app.executeCommand(new CutCommand(app, editor));
app.executeCommand(new PasteCommand(app, editor));
app.undo();
}
}
장점
단점