ch6 - 命令模式

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

44
(五) 使用状态实现撤销
测试
45
(五) 使用状态实现撤销
RUN
46
(五) 使用状态实现撤销
通常,实现撤销,需要记录状态
47
(五) 使用状态实现撤销
CeilingFan
ቤተ መጻሕፍቲ ባይዱ
48
(五) 使用状态实现撤销
CeilingFan(续)
49
(五) 使用状态实现撤销
CeilingFan命令
5
(二) 遥控器
6
(三) 厂商类
7
(三) 厂商类
8
二、对象村餐厅
(一) (二) (三)
餐厅的交互 餐厅的角色和职责 从餐厅到命令模式
9
(一) 餐厅的交互
10
(一) 餐厅的交互
11
(二) 餐厅的角色和职责
12
(二) 餐厅的角色和职责
13
(二) 餐厅的角色和职责
14
(三) 从餐厅到命令模式
31
(二) 实现遥控器
实现命令
32
(二) 实现遥控器
实现命令(续)
33
(三) 测试遥控器
测试遥控器
34
(三) 测试遥控器
测试遥控器(续)
35
(三) 测试遥控器
测试遥控器(续)
36
(三) 测试遥控器
测试遥控器(续)
37
(三) 测试遥控器
空对象
38
(四) 写文档
69
总结
OO Patterns Observer - defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically
6
软件设计方法及设计模式
命令模式
张聚礼 副教授/系统分析师
2
主要内容
一 二 三
遥控器
对象村餐厅 定义命令模式
实现遥控器 使用宏命令 命令模式的更多用途
四 五 六
一、遥控器
(一) (二) (三)
巴斯特家电自动化公司 遥控器 厂商类
4
(一) 巴斯特家电自动化公司
需要一个创新遥控器,提供有关原型类。
当遥控器控制不同的设备呢?
56
(一) 每个遥控器都需要Party模式
57
(一) 每个遥控器都需要Party模式
当遥控器控制不同的设备呢?
58
(二) 使用宏命令
创建宏命令集合
59
(二) 使用宏命令
60
(二) 使用宏命令
创建两个数组
61
(二) 使用宏命令
指定宏命令到按钮
62
24
(四) 定义
25
(五) 类图
26
四、实现遥控器
(一) (二) (三) (四) (五)
指定命令 实现遥控器 测试遥控器 写文档 使用状态实现撤销
27
(一) 指定命令
28
(二) 实现遥控器
实现遥控器
29
(二) 实现遥控器
实现遥控器(续)
30
(二) 实现遥控器
实现遥控器(续)
OO Basics
Abstraction Encapsulation Polymorphism Inheritance
Encapsulate what varies. Favor composition over inheritence. Program to interfaces, not implementations.
39
(五) 使用状态实现撤销
当命令支持撤销时
40
(五) 使用状态实现撤销
从LightOnCommand着手
41
(五) 使用状态实现撤销
(续)
现在,来处理LightOffCommand
42
(五) 使用状态实现撤销
要支持撤销,需要修改遥控器类
43
(五) 使用状态实现撤销
(续)
3
OO Principles
Classes should be open for extension but closed for modification.
71
总结
OO Patterns Abstract Factory Provide an interface for creating families of related or depedent objects without specifying their concrete classes. Factory Method Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to the subclasses.
73
总结
6
OO Patterns Command Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
(一) 第一个命令对象
实现打开电灯的命令
19
(二) 使用命令对象
20
(三) 遥控器的简单测试
21
(三) 遥控器的简单测试
RUN
22
(三) 遥控器的简单测试
23
(四) 定义
命令(Command)模式:将“请求”封装成对象, 以便使用请求、队列或日志来参数化其它对象。 命令模式也支持可撤销的操作。
74
75
4
OO Principles
Depend on abstractions. Do not depend on concrete classes.
72
总结
5
OO Patterns
Singleton - ensures a class has only one instance, and provides a global point of access to it.
(二) 使用宏命令
最后,测试
63
(二) 使用宏命令
RUN
64
六、命令模式的更多用途
(一)
队列请求
(二)
日志请求
65
(一) 队列请求
66
(二) 日志请求
67
七、总结
68
总结
OO Patterns
OO Principles
1
Strategy - defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
15
(三) 从餐厅到命令模式
客户创建一个命令对象

客户利用setCommand()将命令对象储存在调
用者中
客户要求调用者执行命令
16
三、定义命令模式
( 一) ( 二)
(三) (四) (五)
第一个命令对象 使用命令对象
遥控器的简单测试
定义 类图
17
(一) 第一个命令对象
实现命令接口
18
2
OO Principles
Strive for loosely coupled designs between objects that interact.
70
总结
OO Patterns Decorator - Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
50
(五) 使用状态实现撤销
CeilingFan命令(续)
51
(五) 使用状态实现撤销
准备测试
52
(五) 使用状态实现撤销
准备测试(续)
53
(五) 使用状态实现撤销
RUN
54
五、使用宏命令
(一)
每个遥控器都需要Party模式 使用宏命令
(二)
55
(一) 每个遥控器都需要Party模式
相关文档
最新文档