设计模式(12)----- 命令设计模式(升级----一个开关控制多条命令)

我们先来看张类图

RemoteControl类修改一下

public
class RemoteControl {     Command[] onCommands;

Command[] offCommands;

public RemoteControl() {

onCommands =
new Command[7];          offCommands =
new Command[7];

Command noCommand =
new NoCommand();

for (int i =
0; i <
7; i++) {

onCommands[i] = noCommand;

offCommands[i] = noCommand;

}

}

public
void setCommand(int slot, Command onCommand, Command offCommand)

{

onCommands[slot] = onCommand;

offCommands[slot] = offCommand;     }

public
void onButtonWasPushed(int slot) {

onCommands[slot].execute();     }

public
void offButtonWasPushed(int slot) {

offCommands[slot].execute();

}

public
String toString() {

StringBuffer stringBuff =
new StringBuffer();

stringBuff.append("\n------ Remote Control -------\n");

for (int i =
0; i < onCommands.length; i++) {

stringBuff.append("[slot "
+ i +
"] "
+ onCommands[i].getClass().getName()

+
" "

+ offCommands[i].getClass().getName() +
"\n");

}

return stringBuff.toString();

}

}

在开关中加了一个数组

Command[] onCommands; Command[] offCommands;

两个开关一个开的数组和一个关的数组

public void setCommand(int slot, Command onCommand, Command offCommand) {

onCommands[slot] = onCommand;

offCommands[slot] = offCommand;

}

public void onButtonWasPushed(int slot) {

onCommands[slot].execute();

}

public void offButtonWasPushed(int slot) {

offCommands[slot].execute();

}

在设置的时候一个命令控制开一个命令控制关就可以了没有命令对象的方法NoCommand

这条命令对象是一个空方法的实现

public
class NoCommand implements Command {

public
void execute() { }

}

Command代码不变

public
interface Command {

public
void execute();

}

LightOnCommand控制命令对象

public
class LightOnCommand implements Command {     Light light;

public LightOnCommand(Light light) {

this.light = light;

}

public
void execute() {

light.on();

}

}

LightOffCommand控制命令对象

public
class LightOffCommand implements Command {     Light light;


}


public LightOffCommand(Light light) {

this.light = light;

}

public
void execute() {

light.off();

}

具体实现类Light

public
class Light {

String location =
"";

public Light(String location) {

this.location = location;

}

public
void on() {

System.out.println(location +
" light is on");

}

public
void off() {

System.out.println(location +
" light is off");

}

}

cd关闭开关StereoOffCommand

public
class StereoOffCommand implements Command {

Stereo stereo;

public StereoOffCommand(Stereo stereo) {

this.stereo = stereo;

}

public
void execute() {

stereo.off();

}

}

cd打开开关StereoOnWithCDCommand

public
class StereoOnWithCDCommand implements Command {

Stereo stereo;


}


public StereoOnWithCDCommand(Stereo stereo) {

this.stereo = stereo;

}

public
void execute() {     stereo.on();

stereo.setCD();

stereo.setVolume(11);

}

具体实现类cd Stereo


public
class Stereo {     String location;

public Stereo(String location) {

this.location = location;

}

public
void on() {

System.out.println(location +
" stereo is on");

}

public
void off() {

System.out.println(location +
" stereo is off");

}

public
void setCD() {

System.out.println(location +
" stereo is set for CD input");

}

public
void setDVD() {

System.out.println(location +
" stereo is set for DVD input");

}

public
void setRadio() {

System.out.println(location +
" stereo is set for Radio");

}

public
void setVolume(int volume) {

// code to set the volume

// valid range: 1-11 (after all 11 is better than 10, right?)

System.out.println(location +
" Stereo volume set to "
+ volume);

}

}

test测试Test

package com.DesignPatterns.ae.command2;

/**

*

  • @author qingruihappy

  • @data 2018年9月28日 下午10:43:45

  • @说明:

*

  • 一:开关==遥控器==调用者

*

  • 二:怎么用一个开关控制多个一组控件,每个控件中还可能有不同的操作。

  • 一个开关控制多个控件,就是在开关中加入数组(或集合),每个控件有不同的操作,有多少个操作就假如多少个数组

*

  • 三,假如一个按钮按下去会有多个操作的话(比如下面例子中的cd的例子,按下开键会触发

1,打开开关2,设置成cd模式3,默认声音是11.),

  • 那直接在命令的excute方法中调用实例类的三个方法就行了。

*

  • 四:并不是每一个按钮都会设置功能的,如说说现在7个按钮,现在4567号按钮我现在还没想好用在什么地方怎么办呢,那我们预期的是设置成不触发任何方法的按钮

  • 怎么办呢?

  • 现在就是写一个命令对象(NoCommand),同样实现Command但是它的方法是空的,当我们在实例化开关对象(RemoteControl)的时候我们先让数组里面的

  • 元素都默认是这个空对象,假如后面不是空对象的话就会覆盖,假如是空对象的话就不会覆盖了,就是空方法。

*

  • 五:到这里我们就更能深刻的理解接口的作用了,就是定义一个规范,让调用者和被调用者都实现这个接口,从而各干各的事情,互不干涉,但是又可以通过

  • 这个接口达到联系的效果。

  • 我们再来看看之前自己理解的回调函数的韵味:

  • 回调函数既然是让系统调用的,所以你就必须按照系统给定的原型来实现程序

  • 场景:A去调用B,其中A把一些参数穿给了B,B进行了自己的业务逻辑处理之后把返回的结果返给A,但是B怎么返给A呢,就需要B去调用A里的

  • 方法,但是B怎么知道A里面有什么方法呢,因为这一般都是两个程序员的事情,这个时候就需要定义规范了,定义一个接口,B通过这个接口去调用A

  • 里面的方法,而A也必须实现这个接口,这个实现的方法就叫做回调函数,从这里的逻辑我们不难发现,当我们当A去调用B的时候最好把自己的表示符号传

  • 过去,这个时候当B把处理完逻辑之后就可以通过标识符(对象实例)知道调用谁的(A)方法了。

  • */

public
class Test {


public
static
void main(String[] args) {

RemoteControl remoteControl =
new RemoteControl();

Light livingRoomLight =
new Light("Living Room");

Light kitchenLight =
new Light("Kitchen");

CeilingFan ceilingFan =
new CeilingFan("Living Room");

GarageDoor garageDoor =
new GarageDoor("");          Stereo stereo =
new Stereo("Living Room");

LightOnCommand livingRoomLightOn =
new

LightOnCommand(livingRoomLight);

LightOffCommand livingRoomLightOff =
new

LightOffCommand(livingRoomLight);

LightOnCommand kitchenLightOn =
new LightOnCommand(kitchenLight);

LightOffCommand kitchenLightOff =
new LightOffCommand(kitchenLight);

CeilingFanOnCommand ceilingFanOn =
new

CeilingFanOnCommand(ceilingFan);

CeilingFanOffCommand ceilingFanOff =
new CeilingFanOffCommand(ceilingFan);

GarageDoorUpCommand garageDoorUp =
new

GarageDoorUpCommand(garageDoor);

GarageDoorDownCommand garageDoorDown =
new GarageDoorDownCommand(garageDoor);

StereoOnWithCDCommand stereoOnWithCD =
new

StereoOnWithCDCommand(stereo);

StereoOffCommand stereoOff =
new StereoOffCommand(stereo);

remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);

remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);

remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);

remoteControl.setCommand(3, stereoOnWithCD, stereoOff);

System.out.println(remoteControl);

remoteControl.onButtonWasPushed(0);

remoteControl.offButtonWasPushed(0);

remoteControl.onButtonWasPushed(1);

remoteControl.offButtonWasPushed(1);

remoteControl.onButtonWasPushed(2);

remoteControl.offButtonWasPushed(2);

remoteControl.onButtonWasPushed(3);

remoteControl.offButtonWasPushed(3);

}

}

kk

原文地址:https://www.cnblogs.com/qingruihappy/p/9739665.html

时间: 2024-08-30 07:02:22

设计模式(12)----- 命令设计模式(升级----一个开关控制多条命令)的相关文章

在1-15内取一个随机数(一条命令搞定)

1. [[email protected] ~]# echo `expr ${RANDOM} % 14 + 1 ` 10 [[email protected] ~]# 2. [[email protected] ~]# echo $(( ${RANDOM} % 14 + 1 )) 4 [[email protected] ~]# 3. [[email protected] ~]# echo $[ ${RANDOM} % 14 + 1 ] 13 [[email protected] ~]# 4.

Linux常用命令(一)-管理文件和目录命令(2)-ls命令

注:非原创,大部分为摘录 英文全名:List 即列表的意思. 1.命令格式 ls [选项] [目录名] 2.命令功能 列出目标目录中所有的子目录和文件 3.常用参数 -a,–all  列出目录下的所有文件,包括以 . 开头的隐含文件 -A  同-a,但不列出“.”(表示当前目录)和“..”(表示当前目录的父目录). -c   配合 -lt:根据 ctime 排序及显示 ctime (文件状态最后更改的时间)配合 -l:显示 ctime 但根据名称排序否则:根据 ctime 排序 -C  每栏由上

adb命令详解(一)——模拟器相关命令集

对于Android开发者来说,想要测试自己开发的程序在所有Android版本上的效果,拥有一台Android真机是不够,尽管你会刷机,那只会浪费你宝贵的时间,这个时候用ADB工具情况就不一样了,你可以尽情的在所有版本的Android模拟器上测试自己的APP是否达到你期望的效果. 另一种情况就是最新的开发工具Android Studio在启动模拟器的时候要下载HAXM,甚至还要繁琐的配置,如果这个时候不想这么麻烦,那么ADB命令启动模拟器将是你不错的选择.下面我们就介绍所有与模拟器相关的命令. 1

使用 C# (.NET Core) 实现命令设计模式 (Command Pattern)

本文的概念内容来自深入浅出设计模式一书. 项目需求 有这样一个可编程的新型遥控器, 它有7个可编程插槽, 每个插槽可连接不同的家用电器设备. 每个插槽对应两个按钮: 开, 关(ON, OFF). 此外还有一个全局的取消按钮(UNDO). 现在客户想使用这个遥控器来控制不同厂家的家用电器, 例如电灯, 热水器, 风扇, 音响等等. 客户提出让我编写一个接口, 可以让这个遥控器控制插在插槽上的一个或一组设备. 看一下目前各家厂商都有哪些家用电器??: 问题来了, 这些家用电器并没有共同的标准....

设计模式(11)-----命令设计模式

把命令封装成一个命令对象,使请求者和被请求者完全解耦.我们先来看一下类图: 下面我们来看代码 Invoker==SimpleRemoteControl public class SimpleRemoteControl { Command slot;// 有一个插槽持有命令,而这个命令控制着设备 public SimpleRemoteControl() { } public void setCommand(Command command) { slot = command;     } publi

Design Pattern Command 命令设计模式

本设计模式就是利用不同的类包起不同的命令,达到使用什么命令就实现什么操作. 也可以进一步利用map和自己喜欢的命令词对接起来. 一个执行类实际上已经包含了所有需要的操作了,如: class SuperMaker { public: string makeCar() { return "Car"; } string makePlane() { return "Plane"; } }; 这里有两个命令,下面要做的就是使用不同的类把需要的命令包含起来. class Com

命令设计模式

命令设计模式:将一个操作封装到对象中,然后创建多个这种带有不同操作的对象(它们都执行了一个相同接口)并将它们封装到一个类中,然后该类根据不同的入参调用不同的对象执行其对应的操作. 命令设计模式与策略模式的区别 -- 相同点: -- 毫无疑问,第一个相同点肯定是:封装变化.策略模式封装算法的变法,命令模式封装请求的变化. -- 都使用组合来实现功能,达到解耦的目的. -- 如果我们将命令模式做更高层次上的抽象,可以将它看成是一种策略模式.如:我们将Client与 Invoker封装到一起,将Con

iOS常用设计模式——命令设计模式

命令设计模式详解 命令设计模式详解 基本概念 NSInvocation的使用 命令模式的体现 基本概念 命令设计模式将一个请求或行动作封装为对象.这个封装请求比原始的请求要灵活并且可以在对象之前被传递,存储,动态修改或者放进队列里面.苹果公司实现这种模式使用Target-Action机制和Invocation. NSInvocation的使用 在 iOS中可以直接调用 某个对象的消息 方式有2种 一种是performSelector:withObject:再一种就是NSInvocation第一种

12种设计模式c++源码

12种设计源码 源码下载(c++)地址 http://download.csdn.net/detail/lv836735240/7707409 java版 http://download.csdn.net/detail/lv836735240/7707423 12种设计模式c++源码