赞
踩
@Override
public void execute() {
prevSpeed = ceilingFan.getSpeed();
ceilingFan.high();
}
@Override
public void undo() {
if(prevSpeed == CeilingFan.HIGH) {
ceilingFan.high();
} else if(prevSpeed == CeilingFan.MEDIUM) {
ceilingFan.medium();
} else if(prevSpeed == CeilingFan.LOW) {
ceilingFan.low();
} else if(prevSpeed == CeilingFan.OFF) {
ceilingFan.off();
}
}
}
中风速
package undo;
public class CeilingFanMediumCommand implements Command {
CeilingFan ceilingFan;
int prevSpeed;
public CeilingFanMediumCommand(CeilingFan ceilingFan) {
this.ceilingFan = ceilingFan;
}
public void execute() {
prevSpeed = ceilingFan.getSpeed();
ceilingFan.medium();
}
public void undo() {
if (prevSpeed == CeilingFan.HIGH) {
ceilingFan.high();
} else if (prevSpeed == CeilingFan.MEDIUM) {
ceilingFan.medium();
} else if (prevSpeed == CeilingFan.LOW) {
ceilingFan.low();
} else if (prevSpeed == CeilingFan.OFF) {
ceilingFan.off();
}
}
}
低风速
package undo;
public class CeilingFanLowCommand implements Command {
CeilingFan ceilingFan;
int prevSpeed;
public CeilingFanLowCommand(CeilingFan ceilingFan) {
this.ceilingFan = ceilingFan;
}
public void execute() {
prevSpeed = ceilingFan.getSpeed();
ceilingFan.low();
}
public void undo() {
if (prevSpeed == CeilingFan.HIGH) {
ceilingFan.high();
} else if (prevSpeed == CeilingFan.MEDIUM) {
ceilingFan.medium();
} else if (prevSpeed == CeilingFan.LOW) {
ceilingFan.low();
} else if (prevSpeed == CeilingFan.OFF) {
ceilingFan.off();
}
}
}
电灯类
package undo;
public class Light {
String location;
int level;
public Light(String location) {
this.location = location;
}
public void on() {
level = 100;
System.out.println(“Light is on”);
}
public void off() {
level = 0;
System.out.println(“Light is off”);
}
public void dim(int level) {
this.level = level;
if (level == 0) {
off();
}
else {
System.out.println("Light is dimmed to " + level + “%”);
}
}
public int getLevel() {
return level;
}
}
打开电灯
package undo;
public class LightOnCommand implements Command {
Light light;
public LightOnCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.on();
}
@Override
public void undo() {
light.off();
}
}
关闭电灯
package undo;
public class LightOffCommad implements Command {
Light light;
public LightOffCommad(Light light) {
this.light = light;
}
@Override
public void execute() {
light.off();
}
@Override
public void undo() {
light.on();
}
}
无命令
package undo;
public class NoCommand implements Command {
public void execute() { }
public void undo() { }
}
遥控器类
package undo;
public class RemoteControlWithUndo {
Command[] onCommands;
Command[] offCommands;
Command undoCommand;
public RemoteControlWithUndo() {
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;
}
undoCommand = noCommand;
}
public void setCommand(int slot, Command onCommand, Command offCommand) {
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public void onButtonWasPushed(int slot) {
onCommands[slot].execute();
undoCommand = onCommands[slot];
}
public void offButtonWasPushed(int slot) {
offCommands[slot].execute();
undoCommand = offCommands[slot];
}
//当按下撤销按钮,我们调用uodoCommand实例变量的undo()方法,就可以倒转前一个命令
public void undoButtonWasPushed() {
undoCommand.undo();
}
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()
}
stringBuff.append("[undo] " + undoCommand.getClass().getName() + “\n”);
return stringBuff.toString();
}
}
可以开始测试了
package undo;
public class RemoteLoader {
public static void main(String[] args) {
RemoteControlWithUndo remoteControl = new RemoteControlWithUndo();
//第一组测试
Light livingRoomLight = new Light(“Living Room”);
LightOnCommand livingRoomLightOn =
new LightOnCommand(livingRoomLight);
LightOffCommad livingRoomLightOff =
new LightOffCommad(livingRoomLight);
remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
System.out.println(remoteControl);
remoteControl.undoButtonWasPushed();
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(0);
System.out.println(remoteControl);
remoteControl.undoButtonWasPushed();
//第二组测试
CeilingFan ceilingFan = new CeilingFan(“Living Room”);
CeilingFanMediumCommand ceilingFanMedium =
new CeilingFanMediumCommand(ceilingFan);
CeilingFanHighCommand ceilingFanHigh =
new CeilingFanHighCommand(ceilingFan);
CeilingFanOffCommand ceilingFanOff =
new CeilingFanOffCommand(ceilingFan);
remoteControl.setCommand(0, ceilingFanMedium, ceilingFanOff);
remoteControl.setCommand(0, ceilingFanHigh, ceilingFanOff);
remoteControl.onButtonWasPushed(0); //首先以中速开启风扇
remoteControl.offButtonWasPushed(0);//关闭
System.out.println(remoteControl);
remoteControl.undoButtonWasPushed();
remoteControl.onButtonWasPushed(1); //首先以中速开启风扇
System.out.println(remoteControl);
remoteControl.undoButtonWasPushed();
}
}
测试结果:
Light is on
Light is off
------ Remote Control -------
[slot 0] undo.LightOnCommand undo.LightOffCommad
[slot 1] undo.NoCommand undo.NoCommand
[slot 2] undo.NoCommand undo.NoCommand
[slot 3] undo.NoCommand undo.NoCommand
[slot 4] undo.NoCommand undo.NoCommand
[slot 5] undo.NoCommand undo.NoCommand
[slot 6] undo.NoCommand undo.NoCommand
[undo] undo.LightOffCommad
Light is on
Light is off
Light is on
------ Remote Control -------
[slot 0] undo.LightOnCommand undo.LightOffCommad
[slot 1] undo.NoCommand undo.NoCommand
[slot 2] undo.NoCommand undo.NoCommand
[slot 3] undo.NoCommand undo.NoCommand
[slot 4] undo.NoCommand undo.NoCommand
[slot 5] undo.NoCommand undo.NoCommand
[slot 6] undo.NoCommand undo.NoCommand
[undo] undo.LightOnCommand
Light is off
------ Remote Control -------
[slot 0] undo.CeilingFanHighCommand undo.CeilingFanOffCommand
[slot 1] undo.NoCommand undo.NoCommand
[slot 2] undo.NoCommand undo.NoCommand
[slot 3] undo.NoCommand undo.NoCommand
[slot 4] undo.NoCommand undo.NoCommand
[slot 5] undo.NoCommand undo.NoCommand
[slot 6] undo.NoCommand undo.NoCommand
[undo] undo.CeilingFanOffCommand
------ Remote Control -------
[slot 0] undo.CeilingFanHighCommand undo.CeilingFanOffCommand
[slot 1] undo.NoCommand undo.NoCommand
[slot 2] undo.NoCommand undo.NoCommand
[slot 3] undo.NoCommand undo.NoCommand
[slot 4] undo.NoCommand undo.NoCommand
[slot 5] undo.NoCommand undo.NoCommand
[slot 6] undo.NoCommand undo.NoCommand
[undo] undo.NoCommand
4. party (使用宏命令的命令模式)
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
其实要轻松掌握很简单,要点就两个:
你不需要是天才,也不需要具备强悍的天赋,只要做到这两点,短期内成功的概率是非常高的。
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。
下面资料部分截图,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。
附送高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、近期面试跳槽、自身职业规划迷茫的朋友们。
Android核心高级技术PDF资料,BAT大厂面试真题解析;
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算
找到一套好的视频资料,紧跟大牛梳理好的知识框架进行学习。
2. 多练。 (视频优势是互动感强,容易集中注意力)
你不需要是天才,也不需要具备强悍的天赋,只要做到这两点,短期内成功的概率是非常高的。
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。
下面资料部分截图,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。
[外链图片转存中…(img-JXMJES41-1712305657028)]
附送高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、近期面试跳槽、自身职业规划迷茫的朋友们。
Android核心高级技术PDF资料,BAT大厂面试真题解析;
[外链图片转存中…(img-wZCqted0-1712305657029)]
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。