Command Patterns encapsulate a request or a command.
Command Patterns allow us parameterize clients with different commands.
interface Command { void doCommand(); } class MyUndoCommand implements Command { private Receiver receiver; MyUndoCommand(Receiver recv) { receiver = recv;//from ww w .ja v a2 s .co m } @Override public void doCommand() { receiver.performUndo(); } } class MyRedoCommand implements Command { private Receiver receiver; MyRedoCommand(Receiver recv) { receiver = recv; } @Override public void doCommand() { receiver.performRedo(); } } class Receiver { public void performUndo() { System.out.println("Undo Command"); } public void performRedo() { System.out.println("Redo Command"); } } class Invoke { Command cmd; public void executeCommand(Command cmd) { this.cmd = cmd; this.cmd.doCommand(); } } public class Main { public static void main(String[] args) { Receiver editor = new Receiver(); Invoke inv = new Invoke(); MyUndoCommand unCmd = new MyUndoCommand(editor); MyRedoCommand reCmd = new MyRedoCommand(editor); inv.executeCommand(unCmd); inv.executeCommand(reCmd); } }