12
返回列表 发新帖
楼主: 臧圩人

[笔记] 《Head First设计模式》阅读笔记.第六章

[复制链接]
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
11#
 楼主| 发表于 2010-1-19 14:48 | 只看该作者
// 遥控器
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 = new NoCommand();
                }
                for (int i = 0; i < rightCommands.length; i++) {
                        rightCommands = 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();
        }
}

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
12#
 楼主| 发表于 2010-1-19 14:48 | 只看该作者
// 空命令,处理空对象(null object)
public class NoCommand implements Command {
        public void execute() {
        }

        public void undo() {
        }
}

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
13#
 楼主| 发表于 2010-1-19 14:49 | 只看该作者
----命令(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

------------

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
14#
 楼主| 发表于 2010-1-19 14:49 | 只看该作者
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.execute();
                }
        }

        public void undo() {
                for (int i = commands.length - 1; i >= 0; i--) {
                        commands.undo();
                }
        }
}

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
15#
 楼主| 发表于 2010-1-19 14:50 | 只看该作者
----宏命令(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.

--------

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
16#
 楼主| 发表于 2010-1-19 14:50 | 只看该作者
4.聪明命令(Smart Command)实例

在以上代码基础上完成:
// 聪明(Smart)命令实例
public class PrintLogCommand implements Command {
        public void execute() {
                // 自己实现,而不是委托其它类
                System.out.println("Log at " + new Date());
        }

        public void undo() {
                // 命令可以支持撤销,也可以不支持
        }
}

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
17#
 楼主| 发表于 2010-1-19 14:51 | 只看该作者
----聪明命令(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

------------

--END--

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
18#
 楼主| 发表于 2010-1-19 14:52 | 只看该作者
本该昨天完成的,晚了一天,要加紧

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
19#
发表于 2010-1-19 15:45 | 只看该作者
nice job

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表