Design Pattern Explained
- The Command Pattern:
- encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations
-
1 public interface Command { 2 public void execute(); 3 } 4 5 public class LightOnCommand implements Command { 6 Light light; 7 8 public LightOnCommand(Light light) { 9 this.light = light; 10 } 11 12 public void execute() { 13 light.on(); 14 } 15 } 16 17 public class RemoteControl { 18 Command slot; 19 20 public RemoteControl() {} 21 22 public void setCommand(Command slot) { 23 this.slot = slot; 24 } 25 26 public void buttonPressed() { 27 slot.execute(); 28 } 29 }
- x
- x
- x
- x
- x
- x
- x
- x
- x
- x
- x
- x
- x
- x
- x
- x
- x
- x
- x