• 第二节:命令模式——应用


    一、命令模式解决智能生活项目

      1、编写程序,使用命令模式,完成前面的智能家电项目;

      2、思路分析和图解

             

    二、代码实现

      执行者,被调用者:

     1 /**
     2  * 执行者,被调用者
     3  */
     4 public class LightReceiver {
     5 
     6     public void on() {
     7         System.out.println("电灯打开了...");
     8     }
     9 
    10     public void off() {
    11         System.out.println("电灯关闭了...");
    12     }
    13 }
    14 -----------------------------------
    15 public class TVReceiver {
    16     public void on() {
    17         System.out.println("电视打开了...");
    18     }
    19 
    20     public void off() {
    21         System.out.println("电视关闭了...");
    22     }
    23 }

      命令接口:

     1 /**
     2  * 创建命令接口
     3  */
     4 public interface Command {
     5 
     6     /**
     7      * 执行动作(操作)
     8      */
     9     public void execute();
    10 
    11     /**
    12      * 撤销动作(操作)
    13      */
    14     public void undo();
    15 }

      具体命令:(电灯)

     1 public class LightOnCommand implements Command{
     2 
     3     /**
     4      * 聚合一个接收者,被调用者
     5      */
     6     private LightReceiver light;
     7 
     8     public LightOnCommand(LightReceiver lightReceiver) {
     9         this.light = lightReceiver;
    10     }
    11 
    12 
    13     @Override
    14     public void execute() {
    15         //调用接收者方法
    16         light.on();
    17     }
    18 
    19     @Override
    20     public void undo() {
    21         //调用接收者方法
    22         light.off();
    23     }
    24 }
    25 ------------------------------------------
    26 public class LightOffCommand implements Command{
    27     /**
    28      * 聚合一个接收者,被调用者
    29      */
    30     private LightReceiver light;
    31 
    32     public LightOffCommand(LightReceiver lightReceiver) {
    33         this.light = lightReceiver;
    34     }
    35 
    36 
    37     @Override
    38     public void execute() {
    39         //调用接收者方法
    40         light.off();
    41     }
    42 
    43     @Override
    44     public void undo() {
    45         //调用接收者方法
    46         light.on();
    47     }
    48 }
    49 -----------------------------------------
    50 /**
    51  * 没有任何命令,即空执行;用于初始化每个按钮
    52  * 当调用空命令时,对象什么都不做即可
    53  * 其实,这样是一种设计模式,可以省掉对空判断
    54  */
    55 public class NoCommond implements Command{
    56     @Override
    57     public void execute() {
    58 
    59     }
    60 
    61     @Override
    62     public void undo() {
    63 
    64     }
    65 }

      电视具体命令:

     1 public class TVOnCommand implements Command{
     2     /**
     3      * 聚合一个接收者,被调用者
     4      */
     5     private TVReceiver tv;
     6 
     7     public TVOnCommand(TVReceiver tvReceiver) {
     8         this.tv = tvReceiver;
     9     }
    10 
    11 
    12     @Override
    13     public void execute() {
    14         //调用接收者方法
    15         tv.on();
    16     }
    17 
    18     @Override
    19     public void undo() {
    20         //调用接收者方法
    21         tv.off();
    22     }
    23 }
    24 -----------------------------------------
    25 public class TVOffCommand implements Command {
    26     /**
    27      * 聚合一个接收者,被调用者
    28      */
    29     private TVReceiver tv;
    30 
    31     public TVOffCommand(TVReceiver tvReceiver) {
    32         this.tv = tvReceiver;
    33     }
    34 
    35 
    36     @Override
    37     public void execute() {
    38         //调用接收者方法
    39         tv.off();
    40     }
    41 
    42     @Override
    43     public void undo() {
    44         //调用接收者方法
    45         tv.on();
    46     }
    47 }

      调用者(遥控器):

     1 public class RemoteController {
     2 
     3     /**
     4      * 开   按钮的命令数组
     5      */
     6     Command[] onCommands;
     7     Command[] offCommands;
     8 
     9     /**
    10      * 执行撤销的命令
    11      */
    12     Command undoCommand;
    13 
    14     /**
    15      * 构造器,完成对按钮初始化
    16      */
    17     public RemoteController() {
    18         onCommands = new Command[5];
    19         offCommands = new Command[5];
    20 
    21         for (int i = 0; i < 5; i++) {
    22             onCommands[i] = new NoCommond();
    23             offCommands[i] = new NoCommond();
    24         }
    25 
    26     }
    27 
    28     /**
    29      * 给按钮设置需要的命令
    30      */
    31     public void setCommand(int no, Command onCommand, Command offCommand) {
    32         onCommands[no] = onCommand;
    33         offCommands[no] = offCommand;
    34     }
    35 
    36     /**
    37      * 按下开按钮
    38      */
    39     public void onButtonWasPushed(int no) {
    40         //找到按下的开的按钮,并调用对应的方法
    41         onCommands[no].execute();
    42         //记录这次的操作,用于撤销
    43         undoCommand = onCommands[no];
    44     }
    45 
    46     /**
    47      * 按下 关 的按钮
    48      */
    49     public void offButtonWasPushed(int no) {
    50         //找到按下的关的按钮,并调用对应的方法
    51         offCommands[no].execute();
    52         //记录这次的操作,用于撤销
    53         undoCommand = offCommands[no];
    54     }
    55 
    56     /**
    57      * 按下撤销按钮
    58      */
    59     public void undoButtonWasPushed() {
    60         undoCommand.undo();
    61     }
    62 }

      客户端:

     1 public class Client {
     2 
     3     public static void main(String[] args) {
     4         //使用命令设计模式,完成通过遥控器,对电灯的操作
     5         //创建电灯的对象(接收者)
     6         LightReceiver lightReceiver = new LightReceiver();
     7 
     8         //创建电灯相关的开关命令
     9         LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver);
    10         LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver);
    11 
    12         //创建电灯的对象(接收者)
    13         TVReceiver tvReceiver = new TVReceiver();
    14 
    15 
    16         //创建电视相关的开关命令
    17         TVOnCommand tvOnCommand = new TVOnCommand(tvReceiver);
    18         TVOffCommand tvOffCommand = new TVOffCommand(tvReceiver);
    19 
    20         //需要一个遥控器
    21         RemoteController remoteController = new RemoteController();
    22 
    23         //给我们的遥控器设置命令,比如 no = 0 是电灯的开关操作
    24         remoteController.setCommand(0, lightOnCommand, lightOffCommand);
    25         remoteController.setCommand(1, tvOnCommand, tvOffCommand);
    26 
    27 
    28         System.out.println("---按下灯的开按钮---");
    29         remoteController.onButtonWasPushed(0);
    30 
    31         System.out.println("---按下灯关按钮---");
    32         remoteController.offButtonWasPushed(0);
    33 
    34         System.out.println("---按下撤销按钮---");
    35         remoteController.undoButtonWasPushed();
    36 
    37 
    38 
    39         System.out.println("===使用遥控器操作电视机===");
    40         System.out.println("---按下电视机的开按钮---");
    41         remoteController.onButtonWasPushed(1);
    42 
    43         System.out.println("---按下电视机关按钮---");
    44         remoteController.offButtonWasPushed(1);
    45 
    46         System.out.println("---按下撤销按钮---");
    47         remoteController.undoButtonWasPushed();
    48     }
    49 }

      

  • 相关阅读:
    获取Activity中得到焦点的EditText
    SwipeRefreshLayout嵌套ScrollView包裹复杂头布局和RecyclerView
    摄像机识别图片中的手机号
    Glide 加载图片
    反射,元类
    类与实例
    多态
    sys模块理解补充
    python中os模块再回顾
    面向对象之封装
  • 原文地址:https://www.cnblogs.com/niujifei/p/14391369.html
Copyright © 2020-2023  润新知