- 浏览: 348952 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
bsc2xp:
作者的文章不错,但是程序好像有错误:1、StreamFilte ...
Java6.0新特性之StAX--全面解析Java XML分析技术 -
bystander_:
写的真好啊, 牛
JAVA面试题解惑系列合集恢复下载 -
qdp150273:
想下载,怎么下载不了
《JAVA面试题解惑系列合集》PDF电子书下载 -
qdp150273:
不错啊,受益匪浅
《JAVA面试题解惑系列合集》PDF电子书下载 -
miroki:
感谢分享!
JAVA面试题解惑系列合集恢复下载
1.命令(Command)模式部分
命令(Command)模式:将“请求”封装成对象,以便使用请求、队列或日志来参数化其它对象。命令模式也支持可撤销的操作。
*命令对象通过在一个特定接受者上绑定一组动作来封装请求。为实现这一点,命令对象将动作和接受者包进对象中。
*空对象(null object)可以用于返回无意义的对象时,它可以承担处理null的责任。有时候空对象也被视为一种设计模式。
*将命令执行之前的状态记录在命令对象中,这样就可以实现更复杂的撤销操作。
*宏命令(Macro Command)是一个命令队列,它包含了一组实现了同一个命令接口的类。
*命令(Command)模式通过把接受者当作参数来传递,并且让所有的命令对象都实现一个命令接口的方式,实现了针对接口编程,从而在调用者和接受者之间实现了解耦。
*在调用者中用一个堆栈记录连续执行的命令,这样就可以实现每按一次按钮就执行一次撤销操作的连续撤销功能。
*命令(Command)模式将发出请求的对象和执行请求的对象解耦。
*被解耦的对象之间通过命令对象进行沟通,命令对象封装了接受者和一个或一组动作。
*宏命令是命令的一种简单延伸,执行调用多个命令。宏方法也可以支持撤销。
*在实际操作时,很常见地用“聪明”命令对象,也就是直接实现了请求,而不是将工作委托(Delegate)给接收者。
*命令可以被用来实现日志和事物系统。
2.命令(Command)模式实例
3.宏命令(Macro Command)实例
在以上代码基础上完成:
4.聪明命令(Smart Command)实例
在以上代码基础上完成:
--END--
引用
----连连看解答----
女招待->Invoker
快餐厨师->Receiver
orderUp()->execute()
订单->Command
顾客->Client
takeOrder()->setCommand
------------
女招待->Invoker
快餐厨师->Receiver
orderUp()->execute()
订单->Command
顾客->Client
takeOrder()->setCommand
------------
----GarageDoorOpenCommand类---- public class GarageDoorOpenCommand { GarageDoor door; public GarageDoorOpenCommand(GarageDoor door){ this.door = door; } public void execute() { door.up(); } } ------------
命令(Command)模式:将“请求”封装成对象,以便使用请求、队列或日志来参数化其它对象。命令模式也支持可撤销的操作。
*命令对象通过在一个特定接受者上绑定一组动作来封装请求。为实现这一点,命令对象将动作和接受者包进对象中。
*空对象(null object)可以用于返回无意义的对象时,它可以承担处理null的责任。有时候空对象也被视为一种设计模式。
*将命令执行之前的状态记录在命令对象中,这样就可以实现更复杂的撤销操作。
----CeilingFanLowCommand类---- public class CeilingFanLowCommand implements Command { private CeilingFan fan; private int prevSpeed; public CeilingFanLowCommand(CeilingFan fan) { this.fan = fan; } public void execute() { prevSpeed = fan.getSpeed(); fan.low(); } public void undo() { if (prevSpeed == CeilingFan.HIGH) { fan.high(); } else if (prevSpeed == CeilingFan.MEDIUM) { fan.medium(); } else if (prevSpeed == CeilingFan.LOW) { fan.low(); } else if (prevSpeed == CeilingFan.OFF) { fan.off(); } } } ------------ ----CeilingFanMediumCommand类---- public class CeilingFanMediumCommand implements Command { private CeilingFan fan; private int prevSpeed; public CeilingFanMediumCommand(CeilingFan fan) { this.fan = fan; } public void execute() { prevSpeed = fan.getSpeed(); fan.medium(); } public void undo() { if (prevSpeed == CeilingFan.HIGH) { fan.high(); } else if (prevSpeed == CeilingFan.MEDIUM) { fan.medium(); } else if (prevSpeed == CeilingFan.LOW) { fan.low(); } else if (prevSpeed == CeilingFan.OFF) { fan.off(); } } } ------------ ----CeilingFanOffCommand类---- public class CeilingFanOffCommand implements Command { private CeilingFan fan; private int prevSpeed; public CeilingFanOffCommand(CeilingFan fan) { this.fan = fan; } public void execute() { prevSpeed = fan.getSpeed(); fan.off(); } public void undo() { if (prevSpeed == CeilingFan.HIGH) { fan.high(); } else if (prevSpeed == CeilingFan.MEDIUM) { fan.medium(); } else if (prevSpeed == CeilingFan.LOW) { fan.low(); } else if (prevSpeed == CeilingFan.OFF) { fan.off(); } } } ------------
*宏命令(Macro Command)是一个命令队列,它包含了一组实现了同一个命令接口的类。
----Sharpen Your Pencil解答---- Light light = new Light("Living Room"); TV tv = new TV("Living Room"); Stereo stereo = new Stereo("Living Room"); Hottub hottub = new Hottub(); LightOffCommand lightoff = new LightOffCommand(light); TVOffCommand tvoff = new TVOffCommand(tv); StereoOffCommand stereooff = new StereoOffCommand(stereo); HottubOffCommand hottuboff = new HottubOffCommand(hottub); ------------ ----undo()方法---- public void undo() { // 我觉得根据命令执行的顺序反向地撤销更为合理 for(int i = commands.length() - 1; i >= 0; i--) { commands[i].undo(); } } ------------
*命令(Command)模式通过把接受者当作参数来传递,并且让所有的命令对象都实现一个命令接口的方式,实现了针对接口编程,从而在调用者和接受者之间实现了解耦。
*在调用者中用一个堆栈记录连续执行的命令,这样就可以实现每按一次按钮就执行一次撤销操作的连续撤销功能。
*命令(Command)模式将发出请求的对象和执行请求的对象解耦。
*被解耦的对象之间通过命令对象进行沟通,命令对象封装了接受者和一个或一组动作。
*宏命令是命令的一种简单延伸,执行调用多个命令。宏方法也可以支持撤销。
*在实际操作时,很常见地用“聪明”命令对象,也就是直接实现了请求,而不是将工作委托(Delegate)给接收者。
*命令可以被用来实现日志和事物系统。
2.命令(Command)模式实例
// mp3播放器 public class Mp3Player { private String name; public Mp3Player(String n) { this.name = n; } public void playMusic() { System.out.println("Music is playing."); } public void stopMusic() { System.out.println("Music is stopped."); } public String getName() { return name; } } // 收音机 public class Radio { private float frequency = 100;// 频率 private String name; public Radio(String n) { this.name = n; } public void on() { System.out.println("Radio is on."); } public void off() { System.out.println("Radio is off."); } public void setFrequency(float f) { this.frequency = f; System.out.println("Frequency is set to " + f); } public float getFrequency() { return this.frequency; } public String getName() { return name; } } // 命令接口 public interface Command { public void execute(); public void undo(); } // 播放音乐命令 public class PlayMusicCommand implements Command { private Mp3Player player; public PlayMusicCommand(Mp3Player p) { this.player = p; } public void execute() { this.player.playMusic(); } public void undo() { this.player.stopMusic(); } } // 停止音乐命令 public class StopMusicCommand implements Command { private Mp3Player player; public StopMusicCommand(Mp3Player p) { this.player = p; } public void execute() { this.player.stopMusic(); } public void undo() { this.player.playMusic(); } } // 打开收音机命令 public class RadioOnCommand implements Command { private Radio radio; public RadioOnCommand(Radio p) { this.radio = p; } public void execute() { this.radio.on(); } public void undo() { this.radio.off(); } } // 关闭收音机命令 public class RadioOffCommand implements Command { private Radio radio; public RadioOffCommand(Radio p) { this.radio = p; } public void execute() { this.radio.off(); } public void undo() { this.radio.on(); } } // 频率调高命令 public class FrequencyHigherCommand implements Command { private Radio radio; private float prevFre; public FrequencyHigherCommand(Radio p) { this.radio = p; } public void execute() { this.prevFre = radio.getFrequency(); radio.setFrequency(this.prevFre + 0.1f); } public void undo() { this.radio.setFrequency(prevFre); } } // 频率调低命令 public class FrequencylowerCommand implements Command { private Radio radio; private float prevFre; public FrequencylowerCommand(Radio p) { this.radio = p; } public void execute() { this.prevFre = radio.getFrequency(); if (this.prevFre >= 0.1) radio.setFrequency(this.prevFre - 0.1f); } public void undo() { this.radio.setFrequency(prevFre); } } // 遥控器 public class RemoteControl { private Command[] leftCommands; private Command[] rightCommands; private Command prevCommand; public RemoteControl() { leftCommands = new Command[3]; rightCommands = new Command[3]; for (int i = 0; i < leftCommands.length; i++) { leftCommands[i] = new NoCommand(); } for (int i = 0; i < rightCommands.length; i++) { rightCommands[i] = new NoCommand(); } prevCommand = new NoCommand(); } public void setCommand(int index, Command left, Command right) { leftCommands[index] = left; rightCommands[index] = right; } public void leftButtonPressed(int index) { System.out.println("Left button [" + index + "] is pressed."); leftCommands[index].execute(); prevCommand = leftCommands[index]; } public void rightButtonPressed(int index) { System.out.println("Right button [" + index + "] is pressed."); rightCommands[index].execute(); prevCommand = rightCommands[index]; } public void cancelButtonPressed() { System.out.println("Cancel button is pressed."); prevCommand.undo(); } } // 空命令,处理空对象(null object) public class NoCommand implements Command { public void execute() { } public void undo() { } } ----命令(Command)模式测试程序---- // 测试程序 public class TestRemoteControl { public static void main(String[] args) { RemoteControl rc = new RemoteControl(); Mp3Player player = new Mp3Player("Sony Ericsson"); PlayMusicCommand pm = new PlayMusicCommand(player); StopMusicCommand sm = new StopMusicCommand(player); rc.setCommand(0, pm, sm); Radio radio = new Radio("Tecsun"); RadioOnCommand ron = new RadioOnCommand(radio); RadioOffCommand roff = new RadioOffCommand(radio); FrequencyHigherCommand fh = new FrequencyHigherCommand(radio); FrequencylowerCommand fl = new FrequencylowerCommand(radio); rc.setCommand(1, ron, roff); rc.setCommand(2, fh, fl); rc.leftButtonPressed(0); rc.cancelButtonPressed(); rc.leftButtonPressed(0); rc.rightButtonPressed(0); rc.cancelButtonPressed(); rc.leftButtonPressed(1); rc.cancelButtonPressed(); rc.leftButtonPressed(1); rc.rightButtonPressed(1); rc.cancelButtonPressed(); rc.leftButtonPressed(2); rc.cancelButtonPressed(); rc.leftButtonPressed(2); rc.rightButtonPressed(2); rc.cancelButtonPressed(); } }
引用
----测试程序运行结果----
Left button [0] is pressed.
Music is playing.
Cancel button is pressed.
Music is stopped.
Left button [0] is pressed.
Music is playing.
Right button [0] is pressed.
Music is stopped.
Cancel button is pressed.
Music is playing.
Left button [1] is pressed.
Radio is on.
Cancel button is pressed.
Radio is off.
Left button [1] is pressed.
Radio is on.
Right button [1] is pressed.
Radio is off.
Cancel button is pressed.
Radio is on.
Left button [2] is pressed.
Frequency is set to 100.1
Cancel button is pressed.
Frequency is set to 100.0
Left button [2] is pressed.
Frequency is set to 100.1
Right button [2] is pressed.
Frequency is set to 100.0
Cancel button is pressed.
Frequency is set to 100.1
------------
Left button [0] is pressed.
Music is playing.
Cancel button is pressed.
Music is stopped.
Left button [0] is pressed.
Music is playing.
Right button [0] is pressed.
Music is stopped.
Cancel button is pressed.
Music is playing.
Left button [1] is pressed.
Radio is on.
Cancel button is pressed.
Radio is off.
Left button [1] is pressed.
Radio is on.
Right button [1] is pressed.
Radio is off.
Cancel button is pressed.
Radio is on.
Left button [2] is pressed.
Frequency is set to 100.1
Cancel button is pressed.
Frequency is set to 100.0
Left button [2] is pressed.
Frequency is set to 100.1
Right button [2] is pressed.
Frequency is set to 100.0
Cancel button is pressed.
Frequency is set to 100.1
------------
3.宏命令(Macro Command)实例
在以上代码基础上完成:
// 宏命令 public class MacroCommand implements Command { private Command[] commands; public MacroCommand(Command[] commands) { this.commands = commands; } public void execute() { for (int i = 0; i < commands.length; i++) { commands[i].execute(); } } public void undo() { for (int i = commands.length - 1; i >= 0; i--) { commands[i].undo(); } } } ----宏命令(Macro Command)测试程序---- // 测试宏命令 public class TestMacroCommand { public static void main(String[] args) { Mp3Player player = new Mp3Player("Sony Ericsson"); PlayMusicCommand pm = new PlayMusicCommand(player); StopMusicCommand sm = new StopMusicCommand(player); Radio radio = new Radio("Tecsun"); RadioOnCommand ron = new RadioOnCommand(radio); RadioOffCommand roff = new RadioOffCommand(radio); FrequencyHigherCommand fh = new FrequencyHigherCommand(radio); FrequencylowerCommand fl = new FrequencylowerCommand(radio); FrequencyHigherCommand fh2 = new FrequencyHigherCommand(radio); FrequencylowerCommand fl2 = new FrequencylowerCommand(radio); Command[] commands = new Command[] { pm, sm, ron, fh, fh2, fl, fl2, roff }; MacroCommand mc = new MacroCommand(commands); RemoteControl rc = new RemoteControl(); rc.setCommand(0, mc, mc); rc.leftButtonPressed(0); rc.cancelButtonPressed(); } }
引用
----测试程序运行结果----
Left button [0] is pressed.
Music is playing.
Music is stopped.
Radio is on.
Frequency is set to 100.1
Frequency is set to 100.2
Frequency is set to 100.1
Frequency is set to 100.0
Radio is off.
Cancel button is pressed.
Radio is on.
Frequency is set to 100.1
Frequency is set to 100.2
Frequency is set to 100.1
Frequency is set to 100.0
Radio is off.
Music is playing.
Music is stopped.
--------
Left button [0] is pressed.
Music is playing.
Music is stopped.
Radio is on.
Frequency is set to 100.1
Frequency is set to 100.2
Frequency is set to 100.1
Frequency is set to 100.0
Radio is off.
Cancel button is pressed.
Radio is on.
Frequency is set to 100.1
Frequency is set to 100.2
Frequency is set to 100.1
Frequency is set to 100.0
Radio is off.
Music is playing.
Music is stopped.
--------
4.聪明命令(Smart Command)实例
在以上代码基础上完成:
// 聪明(Smart)命令实例 public class PrintLogCommand implements Command { public void execute() { // 自己实现,而不是委托其它类 System.out.println("Log at " + new Date()); } public void undo() { // 命令可以支持撤销,也可以不支持 } } ----聪明命令(Smart Command)测试程序---- // 测试聪明命令 public class TestSmartCommand { public static void main(String[] args) { PrintLogCommand pl = new PrintLogCommand(); RemoteControl rc = new RemoteControl(); rc.setCommand(0, pl, pl); rc.leftButtonPressed(0); } }
引用
----测试程序运行结果----
Left button [0] is pressed.
Log at Tue Jan 19 14:44:02 CST 2010
------------
Left button [0] is pressed.
Log at Tue Jan 19 14:44:02 CST 2010
------------
--END--
发表评论
-
Java6.0新特性之StAX--全面解析Java XML分析技术
2010-04-19 12:57 23816作者:臧圩人(zangweiren) 网址:http://za ... -
《Head First设计模式》阅读笔记.全书总结
2010-04-14 15:13 55181、模式(Pattern)定义 ... -
《Head First设计模式》阅读笔记.其他设计模式
2010-04-14 13:33 22001、附录A部分-剩下的模 ... -
《Head First设计模式》阅读笔记.第十三章
2010-04-13 14:40 19581、与设计模式相处 模式:是在某种情境下(Context), ... -
《Head First设计模式》阅读笔记.第十二章
2010-04-12 15:15 20361、复合(Complex)模式部分 *模式通常被一起使用,并 ... -
《Head First设计模式》阅读笔记.第十一章
2010-04-08 17:57 23761、代理(Proxy)模式部分 实现RMI(远程方法调用)的 ... -
《Head First设计模式》阅读笔记.第十章
2010-04-02 17:22 21981.状态(State)模式部分 *设计谜题 -------- ... -
《Head First设计模式》阅读笔记.第八章
2010-03-30 14:28 23541.模板方法(Template Metho ... -
《Head First设计模式》阅读笔记.第九章
2010-01-27 10:33 25481.迭代器(Iterator)模式 ... -
《Head First设计模式》阅读笔记.第七章-外观模式实例补充
2010-01-21 17:08 2058// 电源 public class Power { p ... -
《Head First设计模式》阅读笔记.第七章
2010-01-21 11:02 22341.适配器(Adapter)模式部分 *OO适配器和真实世界 ... -
《Head First设计模式》阅读笔记.第五章
2010-01-15 16:48 20421.单件(单态,Singleton)模式部分 *有些对象我们 ... -
《Head First设计模式》阅读笔记.第四章
2010-01-14 17:06 24951.简单工厂(Simple Factory ... -
《Head First设计模式》阅读笔记.第三章
2010-01-13 12:07 21191.装饰者(Decorator)模式部分 *利用组合(com ... -
《Head First设计模式》阅读笔记.第二章
2010-01-12 15:04 24491.观察者(Observer)模式部分 *观察者模式:定义了 ... -
《Head First设计模式》阅读笔记.第一章
2010-01-12 10:00 38011.策略模式部分 *面向对象(OO)的四个基本概念是:抽象、 ...
相关推荐
1.1 《Head First设计模式》阅读笔记.第一章 1.2 《Head First设计模式》阅读笔记.第二章 1.3 《Head First设计模式》阅读笔记.第三章 1.4 《Head First设计模式》阅读笔记.第四章 1.5 《Head First设计模式》...
Head.First 设计模式学习笔记.pdf Head.First 设计模式学习笔记.pdf
笔记_HeadFirst设计模式
HeadFirst设计模式学习笔记比较全面详细地讲解了13个设计模式,有利于大家更好的学习HeadFirst设计模式,希望亲们会喜欢~~~
Head First 设计模式学习笔记。更多内容请参见文章内容。
HeadFirst 设计模式学习笔记3--装饰模式 Demo http://blog.csdn.net/laszloyu/archive/2010/05/12/5582561.aspx
HeadFirst设计模式 读书 笔记
HeadFirst 设计模式学习笔记2--观察者模式 demo http://blog.csdn.net/laszloyu/archive/2010/05/12/5581769.aspx
http://blog.csdn.net/laszloyu/archive/2010/05/11/5579765.aspx 示例代码
Head First设计模式读书笔记-DesignPatterns
NULL 博文链接:https://zhangfy068.iteye.com/blog/1706132
著名的《Head First Design ...由于书本过长,整理出笔记帮助回想起设计模式。文件是docx格式,只能由OFFICE Word 2007之后的版本打开,内附Visio类图文件。本文由个人整理摘录,部分内容来自书本,仅供学习使用。
NULL 博文链接:https://chxiaowu.iteye.com/blog/1276845
NULL 博文链接:https://never-say-never.iteye.com/blog/851923
趣味的移动开发讲解讲解,类似个人笔记,非常容易懂的,有好多勾画图
headfirst 设计模式读书笔记 书很好,浅显易懂,例子生动,适合初学者
设计模式Head First学习笔记,以及使用java编写的设计模式源码,Java原生sdk实现23种设计模式
java原始中的设计模式 Gof设计模式 一个Android应用程序,该应用程序演示了以下来源的GoF设计模式的源代码: E.Freeman,E.Freeman,K.Sierra,B.Bates的书:copyright:O'Reilly 2004 布莱恩·汉森(Bryan Hansen),...