设计模式06—命令模式,2024中级前端开发面试解答

System.out.println(location + " ceiling fan is on low");

}

public void off() {

// turns the ceiling fan off

level = 0;

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

}

public int getSpeed() {

return level;

}

}

吊扇的指令:

public class CeilingFanOnCommand implements Command {

CeilingFan ceilingFan;

public CeilingFanOnCommand(CeilingFan ceilingFan) {

this.ceilingFan = ceilingFan;

}

@Override

public void execute() {

ceilingFan.high();

}

}

public class CeilingFanOffCommand implements Command {

CeilingFan ceilingFan;

public CeilingFanOffCommand(CeilingFan ceilingFan) {

this.ceilingFan = ceilingFan;

}

@Override

public void execute() {

ceilingFan.off();

}

}

对Light类的修改如下:

public class Light {

/灯的位置:比如卧室的灯,客厅的灯/

private 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");

}

}

关灯指令:

public class LightOffCommand implements Command {

Light light;

public LightOffCommand(Light light) {

this.light = light;

}

@Override

public void execute() {

light.off();

}

}

GarageDoor:

public class GarageDoor {

private String location;

public GarageDoor() {

}

public GarageDoor(String location) {

this.location = location;

}

public void up() {

System.out.println(location + " Garage Door is Open");

}

public void down() {

System.out.println(location + " Garage Door is Closed");

}

public void stop() {

System.out.println(location + " Garage Door is Stopped");

}

public void lightOn() {

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

}

public void lightOff() {

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

}

}

关门指令:

public class GarageDoorDownCommand implements Command {

GarageDoor garageDoor;

public GarageDoorDownCommand(GarageDoor garageDoor) {

this.garageDoor = garageDoor;

}

@Override

public void execute() {

garageDoor.down();

}

}

接下来开始测试遥控器功能

public class RemoteLoader {

public static void main(String[] args) {

/声明遥控对象/

Light livingRoomLight = new Light(“卧室”);

Light kitchenLight = new Light(“厨房”);

CeilingFan ceilingFan = new CeilingFan(“卧室”);

GarageDoor garageDoor = new GarageDoor(“”);

Stereo stereo = new Stereo(“客厅”);

/创建电灯指令对象/

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);

/创建车库的开与关指令/

GarageDoorOpenCommand garageDoorOpen = new GarageDoorOpenCommand(garageDoor);

GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand(garageDoor);

/创建音响的开与关指令/

StereoOnWithCDCommand stereoOpen = new StereoOnWithCDCommand(stereo);

StereoOffCommand stereoOff = new StereoOffCommand(stereo);

/*现在已经有了全部的命令,接下来将命令安装到遥控器的插槽中/

RemoteControl remoteControl = new RemoteControl();//声明一个遥控器对象

remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);

remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);

remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);

remoteControl.setCommand(3, garageDoorOpen, garageDoorDown);

remoteControl.setCommand(4, stereoOpen, stereoOff);

System.out.println(remoteControl.toString());

/一切准备就绪接下来开始逐个点击遥控器按钮/

remoteControl.onButtonWasPushed(0);

remoteControl.offButtonWasPushed(0);

remoteControl.onButtonWasPushed(1);

remoteControl.offButtonWasPushed(1);

remoteControl.onButtonWasPushed(2);

remoteControl.offButtonWasPushed(2);

remoteControl.onButtonWasPushed(3);

remoteControl.offButtonWasPushed(3);

remoteControl.onButtonWasPushed(4);

remoteControl.offButtonWasPushed(4);

}

}

运行结果:

在这里插入图片描述

写到此处我们可能忽略了我们使用的NoCommand

在这里插入图片描述

NoCommand对象是一个空对象(null object)的例子。

当你不想返回一个有意义的对象时,空对象就很有用。客户也可以将处理null的责任转移给空对象。

举例来说,遥控器不可能一出厂就设置了有意义的命令对象,所以提供了NoCommand对象作为代用品,当调用它的execute()方法时,这种对象什么事情都不做。

在许多设计模式中,都会看到空对象的使用。甚至有些时候,空对象本身也被视为是一种设计模模式

下面便是我们刚刚实现一系列命令的流程:

在这里插入图片描述

撤销功能

=================================================================

我们现在需要在遥控器上加上撤销的功能。

这个功能使用起来就像是这样的:比方说客厅的电灯是关闭的,然后你按下遥控器上的开启按钮,自然电灯就被打开了。现在如果按下撤销按钮,那么上一个动作将被倒转,在这个例子里,电灯将被关闭。

在进入更复杂的例子之前,先让撤销按钮能够处理电灯:

  • 1.当命令支持撤销时,该命令就必须提供和execute()方法相反的undo()方法。不管execute()刚才做什么,undo()都会倒转过来。在各个命令加入undo之前,我们必须先在Command接口中加入undo()方法:

public interface Command {

public void execute();

public void undo();

}

  • 2.我们从LightOnCommand开始下手:如果LightOnCommand的execute(方法被调用,那么最后被调用的是on(方法。我们知道undo()需要调用off()方法进行相反的动作。

public class LightOnCommand implements Command {

Light light;

public LightOnCommand(Light light) {

this.light = light;

}

@Override

public void execute() {

light.on();

}

/**

  • execute执行的是打开灯,所以undo应该执行的是关闭灯

*/

@Override

public void undo() {

light.off();

}

}

接下来继续处理LightOffCommand

public class LightOffCommand implements Command {

Light light;

public LightOffCommand(Light light) {

this.light = light;

}

@Override

public void execute() {

light.off();

}

@Override

public void undo() {

light.on();

}

}

到此事情并没有结束。因为我们还需要让遥控器能够追踪最后被按下的按钮是什么

  • 3.要加上对撤销按钮的支持,我们必须对遥控器类做些小修改。我们打算这么做:加入一个新的实例变量,用来追踪最后被调用的命令,然后,不管何时撒销按钮被按下,我们都可以取出这个命令并调用它的undo()方法。

public class RemoteControlWithUndo {

Command[] onCommands;

Command[] offCommands;

Command undoCommand;//用于记录上一个操作的命令

private final Integer commandNums = 7;

public RemoteControlWithUndo() {

onCommands = new Command[commandNums];

offCommands = new Command[commandNums];

Command noCommand = new NoCommand();

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

onCommands[i] = noCommand;

offCommands[i] = noCommand;

}

undoCommand = noCommand;//一开始,并没有任何前一个命令,所以设置NoCommand

}

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

onCommands[slot] = onCommand;

offCommands[slot] = offCommand;

}

/**

  • 当控下控钮,我们取得这个命令.并优先执行它,然后将它记录在undoCommand实例变量中。

  • 不管是是“开”或“关”命合,我们的处理方法都是一样的。

  • @param solt

*/

public void onButtonWasPushed(int solt) {

onCommands[solt].execute();

undoCommand = onCommands[solt];

}

public void offButtonWasPushed(int solt) {

offCommands[solt].execute();

undoCommand = offCommands[solt];

}

/**

  • 当控下撤销按钮时﹒我们调用undoCoamand实例变量的undo()方法.就可以倒转前一个命令。

  • @param solt

*/

public void undoButtonWasPushed(int solt) {

undoCommand.undo();

}

}

接下来我们进行测试

public class RemoteLoader {

public static void main(String[] args) {

Light livingRoomLight = new Light(“卧室”);//声明电灯实例

/创建开和关两个电灯指令/

LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);

LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);

RemoteControlWithUndo remoteControl = new RemoteControlWithUndo();//声明一个遥控器

remoteControl.setCommand(0, livingRoomLightOn,

livingRoomLightOff);//将电灯的命令设置到0号插槽

/下面依次执行开灯,关灯,撤销按钮/

System.out.println(“----------------开灯,关灯,撤销----------------”);

remoteControl.onButtonWasPushed(0);

remoteControl.offButtonWasPushed(0);

remoteControl.undoButtonWasPushed(0);

/下面依次执行关灯,开灯,撤销按钮/

System.out.println(“----------------关灯,开灯,撤销----------------”);

remoteControl.offButtonWasPushed(0);

remoteControl.onButtonWasPushed(0);

remoteControl.undoButtonWasPushed(0);

}

}

运行结果:

在这里插入图片描述

使用状态的撤销

====================================================================

接下来我们实现一个更有趣的例子,比方说厂商类中的天花板的吊扇,吊扇允许有多种转速,当然也允许被关闭。

吊扇代码:

public class CeilingFan {

/**

  • 档位的转速

**/

public static final Integer HIGH = 3;

public static final Integer MEDIUM = 2;

public static final Integer LOW = 1;

public static final Integer OFF = 0;

private String location;//风扇的位置(比如卧室,客厅等)

private Integer speed;//风扇当前的转速

public CeilingFan(String location) {

this.location = location;

speed = OFF;

}

/**

  • 设置高转速

*/

public void setHigh() {

speed = HIGH;

System.out.println(location + " 风扇档位调节至高速");

}

/**

  • 设置中等转速

*/

public void setMedium() {

speed = MEDIUM;

System.out.println(location + " 风扇档位调节至中速");

}

/**

  • 设置低转速

*/

public void setLow() {

speed = HIGH;

System.out.println(location + " 风扇档位调节至低速");

}

/**

  • 关闭风扇

*/

public void setOff() {

speed = OFF;

System.out.println(location + " 风扇关闭");

}

/**

  • 获取当前转速

  • @return

*/

public Integer getSpeed() {

System.out.println(location + " 风扇转速为:" + speed);

return this.speed;

}

}

吊扇指令

/**

  • 高速指令

*/

public class CeilingFanHighCommand implements Command {

CeilingFan ceilingFan;

int preSpeed;//增加局部状态以便追踪吊扇之前的状态

public CeilingFanHighCommand(CeilingFan ceilingFan) {

this.ceilingFan = ceilingFan;

}

@Override

public void execute() {

/*在execute()中,

在我们改变吊扇的速度之前,需先将它之前的状态记录起来,以便需要撤销时使用。*/

preSpeed = ceilingFan.getSpeed();

ceilingFan.setHigh();

}

/**

  • 将吊扇的速度设置回之前的值达到撤销的目的

*/

@Override

public void undo() {

if (preSpeed == CeilingFan.HIGH) {

ceilingFan.setHigh();

} else if (preSpeed == CeilingFan.MEDIUM) {

ceilingFan.setMedium();

} else if (preSpeed == CeilingFan.LOW) {

ceilingFan.setLow();

} else if (preSpeed == CeilingFan.OFF) {

ceilingFan.setOff();

}

}

}

对于中速,低速,关闭的命令我们只需要仿照着高速的指令然后修改excute()方法中的风扇速度即可,修改方式如下:

在这里插入图片描述

接下来我们开始测试:

public class RemoteLoader_CeilingFan {

public static void main(String[] args) {

/创建风扇对象/

CeilingFan ceilingFan = new CeilingFan(“卧室”);

/创建风扇的具体命令/

CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan);

CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan);

CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);

/创建遥控器/

RemoteControlWithUndo remoteControl = new RemoteControlWithUndo();

/将指令放入遥控器的插槽/

remoteControl.setCommand(0, ceilingFanMedium, ceilingFanOff);

remoteControl.setCommand(1, ceilingFanHigh, ceilingFanOff);

/依次点击遥控器按钮/

System.out.println(“---------------中速开,关闭,回退---------------”);

remoteControl.onButtonWasPushed(0);

remoteControl.offButtonWasPushed(0);

remoteControl.undoButtonWasPushed();

System.out.println(“---------------高速开,回退---------------”);

remoteControl.onButtonWasPushed(1);

remoteControl.undoButtonWasPushed();

}

}

运行结果:

在这里插入图片描述

宏命令

================================================================

我们接下来希望拥有一个遥控器,可以实现按下一个按钮,就同时能弄暗灯光、打开音响和电视、设置好DVD,并让热水器开始加温

我们首先最容易想到的就是制造一种新的命令用来执行其他一堆命令

public class MacroCommand implements Command {

Command[] commands;

/**

  • 在宏命令中,用命令数组存储一大堆命令

  • @param commands

*/

public MacroCommand(Command[] commands) {

this.commands = commands;

}

/**

  • 当这个宏命令被遥控器执行时,就一次性执行数组里的每一个命令

*/

@Override

public void execute() {

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

commands[i].execute();

}

}

@Override

public void undo() {

}

}

使用宏命令


  • TV

/**

  • 电视

*/

public class TV {

String location;//电视位置

int channel;//电视播放的频道

public TV(String location) {

this.location = location;

}

/**

  • 开电视

*/

public void on() {

System.out.println(location + " 电视已打开");

}

public void off() {

System.out.println(location + " 电视已关闭");

}

/**

  • 设置输入源

*/

public void setInputChannel() {

this.channel = 3;

System.out.println(location + " 电视频道输入源设置为DVD");

}

}

public class TVOnCommand implements Command {

TV tv;

public TVOnCommand(TV tv) {

this.tv = tv;

}

@Override

public void execute() {

tv.on();

tv.setInputChannel();

}

@Override

public void undo() {

tv.off();

}

}

public class TVOffCommand implements Command {

TV tv;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注前端)
img

文末

js前端的重头戏,值得花大部分时间学习。

JavaScript知识

推荐通过书籍学习,《 JavaScript 高级程序设计(第 4 版)》你值得拥有。整本书内容质量都很高,尤其是前十章语言基础部分,建议多读几遍。

前端电子书

CodeChina开源项目:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

另外,大推一个网上教程 现代 JavaScript 教程 ,文章深入浅出,很容易理解,上面的内容几乎都是重点,而且充分发挥了网上教程的时效性和资料链接。

学习资料在精不在多,二者结合,定能构建你的 JavaScript 知识体系。

面试本质也是考试,面试题就起到很好的考纲作用。想要取得优秀的面试成绩,刷面试题是必须的,除非你样样精通。

这是288页的前端面试题

介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-Rj0wdXrJ-1711879171706)]
[外链图片转存中…(img-N47SqMoL-1711879171707)]
[外链图片转存中…(img-THb4IsS7-1711879171707)]
[外链图片转存中…(img-kXRRFDbV-1711879171707)]
[外链图片转存中…(img-FfAxBSgQ-1711879171708)]
[外链图片转存中…(img-yL3IvUFP-1711879171708)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注前端)
[外链图片转存中…(img-oBqOaM7e-1711879171709)]

文末

js前端的重头戏,值得花大部分时间学习。

JavaScript知识

推荐通过书籍学习,《 JavaScript 高级程序设计(第 4 版)》你值得拥有。整本书内容质量都很高,尤其是前十章语言基础部分,建议多读几遍。

前端电子书

CodeChina开源项目:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

另外,大推一个网上教程 现代 JavaScript 教程 ,文章深入浅出,很容易理解,上面的内容几乎都是重点,而且充分发挥了网上教程的时效性和资料链接。

学习资料在精不在多,二者结合,定能构建你的 JavaScript 知识体系。

面试本质也是考试,面试题就起到很好的考纲作用。想要取得优秀的面试成绩,刷面试题是必须的,除非你样样精通。

这是288页的前端面试题

288页面试题

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值