Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteWithUndoTest[转]
命令模式可以很轻松的实现撤销(Undo)功能。
命令的接受者:
1unit uReceiveObject;
2
3interface
4
5type
6 TLight = class(TObject)
7 public
8 procedure Open;
9 procedure Off;
10 end;
11
12implementation
13
14{ TLight }
15
16procedure TLight.Off;
17begin
18 Writeln('Light is off.');
19end;
20
21procedure TLight.Open;
22begin
23 Writeln('Light is on.');
24end;
25
26end.
27
命令对象:
1unit uCommandObject;
2
3interface
4
5uses
6 uReceiveObject;
7
8type
9 TCommand = class(TObject)
10 public
11 procedure Execute; virtual; abstract;
12 procedure Undo; virtual; abstract;
13 end;
14
15 TLightOnCommand = class(TCommand)
16 private
17 FLight: TLight;
18 public
19 constructor Create(aLight: TLight);
20 procedure Execute; override;
21 procedure Undo; override;
22 end;
23
24 TLightOffCommand = class(TCommand)
25 private
26 FLight: TLight;
27 public
28 constructor Create(aLight: TLight);
29 procedure Execute; override;
30 procedure Undo; override;
31 end;
32
33implementation
34
35{ TLightOnCommand }
36
37constructor TLightOnCommand.Create(aLight: TLight);
38begin
39 FLight := aLight;
40end;
41
42procedure TLightOnCommand.Execute;
43begin
44 FLight.Open;
45end;
46
47procedure TLightOnCommand.Undo;
48begin
49 FLight.Off;
50end;
51
52{ TLightOffCommand }
53
54constructor TLightOffCommand.Create(aLight: TLight);
55begin
56 FLight := aLight;
57end;
58
59procedure TLightOffCommand.Execute;
60begin
61 FLight.Off;
62end;
63
64procedure TLightOffCommand.Undo;
65begin
66 FLight.Open;
67end;
68
69end.
70
命令的请求者:
1unit uSimpleRemoteWithUndo;
2
3interface
4
5uses
6 uCommandObject;
7
8type
9 TSimpleRemoteWithUndo = class(TObject)
10 private
11 FOnCommand : TCommand;
12 FOffCommand : TCommand;
13 FUndoCommand: TCommand;
14 public
15 procedure SetCommand(aOnCommand, aOffCommand: TCommand);
16 procedure OnButtonWasPressed;
17 procedure OffButtonWasPressed;
18 procedure UndoButtonWasPressed;
19 end;
20
21implementation
22
23{ TSimpleRemoteWithUndo }
24
25procedure TSimpleRemoteWithUndo.OffButtonWasPressed;
26begin
27 FOffCommand.Execute;
28 FUndoCommand := FOffCommand;
29end;
30
31procedure TSimpleRemoteWithUndo.OnButtonWasPressed;
32begin
33 FOnCommand.Execute;
34 FUndoCommand := FOnCommand;
35end;
36
37procedure TSimpleRemoteWithUndo.SetCommand(aOnCommand, aOffCommand: TCommand);
38begin
39 FOnCommand := aOnCommand;
40 FOffCommand := aOffCommand;
41end;
42
43procedure TSimpleRemoteWithUndo.UndoButtonWasPressed;
44begin
45 FUndoCommand.Undo;
46end;
47
48end.
49
客户端,创建具体的命令对象:
1program pSimpleRemoteWithUndoTest;
2
3{$APPTYPE CONSOLE}
4
5uses
6 uSimpleRemoteWithUndo in 'uSimpleRemoteWithUndo.pas',
7 uCommandObject in 'uCommandObject.pas',
8 uReceiveObject in 'uReceiveObject.pas';
9
10var
11 Remote: TSimpleRemoteWithUndo;
12 Light : TLight;
13 LightOnCommand : TCommand;
14 LightOffCommand: TCommand;
15
16begin
17 Remote := TSimpleRemoteWithUndo.Create;
18
19 Light := TLight.Create;
20
21 LightOnCommand := TLightOnCommand.Create(Light);
22 LightOffCommand := TLightOffCommand.Create(Light);
23
24 Remote.SetCommand(LightOnCommand, LightOffCommand);
25
26 Remote.OnButtonWasPressed;
27 Remote.OffButtonWasPressed;
28 Remote.UndoButtonWasPressed;
29 Writeln;
30 Remote.OffButtonWasPressed;
31 Remote.OnButtonWasPressed;
32 Remote.UndoButtonWasPressed;
33
34 Remote.Free;
35 Light.Free;
36 LightOnCommand.Free;
37 LightOffCommand.Free;
38
39 Readln;
40end.
41
运行结果:
Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteWithUndoTest[转]的更多相关文章
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之RemoteControlTest[转]
1 2{<HeadFirst设计模式>之命令模式 } 3{ 本单元中的类为命令的接收者 } 4{ 编译工具 :Delphi7.0 } 5{ 联 ...
- .NET设计模式(17):命令模式(Command Pattern)(转)
概述 在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”.但在某些场合,比如要对行为进行“记录.撤销/重做.事务”等处理,这种无法抵御变化的紧耦合是不合适的.在这种情况下,如何将“行为 ...
- 设计模式之第14章-命令模式(Java实现)
设计模式之第14章-命令模式(Java实现) “小明,滚出去.”“小明,这个问题怎么做?”(可怜的小明无奈躺枪.小明:老师,我和你有什么仇什么怨,我和你有什么仇什么怨啊到底...老师:小明,滚出去.习 ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---工厂模式之简单工厂
简单工厂:工厂依据传进的参数创建相应的产品. http://www.cnblogs.com/DelphiDesignPatterns/archive/2009/07/24/1530536.html { ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook[转]
模板方法模式定义了一个算法骨架,允许子类对算法的某个或某些步骤进行重写(override). 1 2{<HeadFirst设计模式>之模板方法模式 } 3{ 编译工具: Del ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---迭代器模式之DinerMenu[转]
容器的主要职责有两个:存放元素和浏览元素.根据单一职责原则(SRP)要将二者分开,于是将浏览功能打包封装就有了迭代器. 用迭代器封装对动态数组的遍历: 1 2{<HeadFirst设计模式& ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---策略模式之MiniDuckSimulator[转]
1 2{<HeadFirst设计模式>之策略模式 } 3{ 本单元中的类为策略类 } 4{ 编译工具: Delphi7.0 } 5{ E- ...
- C#设计模式学习笔记:(14)命令模式
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7873322.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第二个模式--命 ...
- 设计模式(十五)——命令模式(Spring框架的JdbcTemplate源码分析)
1 智能生活项目需求 看一个具体的需求 1) 我们买了一套智能家电,有照明灯.风扇.冰箱.洗衣机,我们只要在手机上安装 app 就可以控制对这些家电工作. 2) 这些智能家电来自不同的厂家,我们不想针 ...
随机推荐
- ie中报错---不能执行已释放 Script 的代码
我的原因:使用jquery.colorbox.js.在页面中使用弹框,对于父页面中的变量进行修改(弹框页面的js--window.parent.obj.arr= 数组;), 当弹框关闭之后,在父页面中 ...
- 廖雪峰Java11多线程编程-3高级concurrent包-2ReadWriteLock
ReentrantLock保证单一线程执行 ReentrantLock保证了只有一个线程可以执行临界区代码: 临界区代码:任何时候只有1个线程可以执行的代码块. 临界区指的是一个访问共用资源(例如:共 ...
- 0821NOIP模拟测试赛后总结
60分rank20.挂.完. 赛时状态 不是很好.老眼混花看错无数题目信息. 倒不是很困.尽管昨天晚上为了某个该死的s-h-s-j活动报告忙到了今天,但我不得不说车上的睡眠还是挺好的. 照例通读三道题 ...
- maven相互依赖导致无法编译成功
起初是新加了个模块,启动前编译时error,提示找不到依赖模块的类,但java文件上是没有报错的. 后经过排查,发现是循环依赖导致的此问题. 如图,弹出框中有循环依赖的模块会显示红色,右键Open M ...
- 使用Parallel.Invoke并行你的代码
优势和劣势 使用Parallel.Invoke的优势就是使用它执行很多的方法很简单,而不用担心任务或者线程的问题.然而,它并不是适合所有的场景.Parallel.Invoke有很多的劣势 如果你使用它 ...
- PAT甲级——A1003Emergency
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- 关于Collection接口和Map
Iterable才是Collection的父接口.不是Iterator. Map,SortedMap属于接口类型,不可以new的方式创建对象. HashMap基于哈希表实现Map接口的类,并允许nul ...
- AutoMapper简介
先说说DTO DTO是个什么东东? DTO(Data Transfer Object)就是数据传输对象,说白了就是一个对象,只不过里边全是数据而已. 为什么要用DTO? 1.DTO更注重数据,对领域对 ...
- Linux-c glib库hash表GHashTable介绍
百度云glib 链接:https://pan.baidu.com/s/1W9qdlMKWRKIFykenTVuWNQ 密码:ol6y hash表是一种提供key-value访问的数据结构,通过指定的 ...
- C++ 静态对象
一:什么是静态对象? 对象的存储方式是静态的. 局部静态对象和类的静态对象. 局部静态对象:一个变量在函数内部定义,其生命周期跨越了该函数的多次调用.局部对象确保不迟于 ...