Design Pattern Explained

  1. 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 }

       

  2. x
  3. x
  4. x
  5. x
  6. x
  7. x
  8. x
  9. x
  10. x
  11. x
  12. x
  13. x
  14. x
  15. x
  16. x
  17. x
  18. x
  19. x
  20. x
posted @ 2015-07-09 04:13  wxwcase  阅读(140)  评论(0编辑  收藏  举报