博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式(6)--命令模式
阅读量:6984 次
发布时间:2019-06-27

本文共 2703 字,大约阅读时间需要 9 分钟。

命令模式应用场景

  比如有个这样的场景,一个控制器,上面有很多对按钮,一对按钮包括开和关,然后这个可以将一些电器插在这个上面,然后就可以通过这个控制器来进行控制相应的电器。如果以后类似于需要模拟这种场景,你可以参考下这种模式。

命令模式使用步骤

  1.定义一个命令接口,所有命令都需要是现在这个接口,其主要方法有execute,即执行命令。

public interface Command {    public void execute();}

  2.定义一个电器接口,所有电器实现这个接口,方法主要是有on和off,即打开和关闭。

public interface Device {    public void on();    public void off();}

  3.编写一些接口实现类,如灯泡、收音机等及其相关命令实现类

//Light.javapublic class Light implements Device {    private String deviceName;    public Light() {        deviceName = "Light";    }    @Override    public void on() {
System.out.println(deviceName + " is twinkling"); } @Override public void off() {
System.out.println(deviceName + " is off"); }}//Radio.javapublic class Radio implements Device { private String deviceName; public Radio() { deviceName = "radio"; } @Override public void on() { System.out.println(deviceName + "is working"); } @Override public void off() { System.out.println(deviceName + "is over"); }}//CommandOn.javapublic class CommandOn implements Command { private Device device; public CommandOn(Device device) { this.device = device; } @Override public void execute() { device.on(); }}//CommandOff.javapublic class CommandOff implements Command { private Device device; public CommandOff(Device device) { this.device = device; } @Override public void execute() { device.off(); }}

  4.编写控制器

public class RemoteController {    private Command[] commandsOn;    private Command[] commandsOff;    public RemoteController() {        commandsOn = new Command[7];        commandsOff = new Command[7];    }    public void setCommand(int index, Command on, Command off) {        commandsOn[index] = on;        commandsOff[index] = off;    }    public void onButton(int index) {        if (commandsOn[index] != null)            commandsOn[index].execute();    }    public void offButton(int index) {        if (commandsOff[index] != null)            commandsOff[index].execute();    }}

  测试代码

public class Test {    public static void main(String[] args) {        RemoteController remoteController = new RemoteController();        Light light = new Light();        Radio radio = new Radio();        remoteController.setCommand(0, new CommandOn(light), new CommandOff(light));        remoteController.onButton(0);        remoteController.offButton(0);        remoteController.setCommand(0, new CommandOn(radio), new CommandOff(radio));        remoteController.onButton(0);        remoteController.offButton(0);    }}

  如果你对这篇博客有什么建议或者疑问可以留言哦>V<

转载于:https://www.cnblogs.com/simfg/p/6799196.html

你可能感兴趣的文章
用 Hasor 谈一谈MVC设计模式
查看>>
IE 条件注释
查看>>
Windows热键注册(反汇编方法 查看win32api 原理)
查看>>
UNREFERENCED_PARAMETER的作用
查看>>
PHP计算表达式-栈
查看>>
IBATIS中关于iterate"$"与"#"的应用
查看>>
为什么要将对象序列化
查看>>
新增网址/网页 截图api[增加安全防护本接口已停用]源码可下载
查看>>
SpringMVC+Hibernate +MySql+ EasyUI实现POI导出Excel(二)
查看>>
刷leetcode第705题- 设计哈希集合
查看>>
dubbo协议参考
查看>>
SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
查看>>
读《白帽子讲Web安全》之安全意识篇(一)
查看>>
GLSL三种修饰符区别与用途(uniform,attribute和varying)
查看>>
python django django-debug-toolbar 加载缓慢,不能使用。
查看>>
操作系之进程调度及算法详解
查看>>
PHPexcel实列
查看>>
Butterknife 的简单使用 和 配合 Butterknife的插件 Zelezny
查看>>
Magento利用input type=”file”上传图片
查看>>
Android音频开发(4):如何存储和解析wav文件
查看>>