Delphi 设计模式:《HeadFirst设计模式》Delphi7代码
命令模式可以很轻松的实现撤销(Undo)功能。
1. 命令的接受者
unit uReceiveObject;
interface
type
TLight = class(TObject)
public
procedure Open;
procedure Off;
end;
implementation
{ TLight }
procedure TLight.Off;
begin
Writeln('Light is off.');
end;
procedure TLight.Open;
begin
Writeln('Light is on.');
end;
end.
2.命令对象
unit uReceiveObject;
interface
type
TLight = class(TObject)
public
procedure Open;
procedure Off;
end;
implementation
{ TLight }
procedure TLight.Off;
begin
Writeln('Light is off.');
end;
procedure TLight.Open;
begin
Writeln('Light is on.');
end;
end.
3.命令的请求者
unit uSimpleRemoteWithUndo;
interface
uses
uCommandObject;
type
TSimpleRemoteWithUndo = class(TObject)
private
FOnCommand : TCommand;
FOffCommand : TCommand;
FUndoCommand: TCommand;
public
procedure SetCommand(aOnCommand, aOffCommand: TCommand);
procedure OnButtonWasPressed;
procedure OffButtonWasPressed;
procedure UndoButtonWasPressed;
end;
implementation
{ TSimpleRemoteWithUndo }
procedure TSimpleRemoteWithUndo.OffButtonWasPressed;
begin
FOffCommand.Execute;
FUndoCommand := FOffCommand;
end;
procedure TSimpleRemoteWithUndo.OnButtonWasPressed;
begin
FOnCommand.Execute;
FUndoCommand := FOnCommand;
end;
procedure TSimpleRemoteWithUndo.SetCommand(aOnCommand, aOffCommand: TCommand);
begin
FOnCommand := aOnCommand;
FOffCommand := aOffCommand;
end;
procedure TSimpleRemoteWithUndo.UndoButtonWasPressed;
begin
FUndoCommand.Undo;
end;
end.
4.客户端,创建具体的命令
program pSimpleRemoteWithUndoTest;
{$APPTYPE CONSOLE}
uses
uSimpleRemoteWithUndo in 'uSimpleRemoteWithUndo.pas',
uCommandObject in 'uCommandObject.pas',
uReceiveObje
相关文档:
设置图像关键颜色,使图像的某种或某个范围的颜色成为透明色,是图片合成、动画显示中经常用的图像处理手段。下面是实现代码:
过程定义:
// 设置色键(透明范围)。colorLow 低色键值; colorHigh 高色键值
// 当像素A、R、G、B值同时大于等于colorLow和小于等于colorHigh时为透明色
procedu ......
本文是基于《GDI+在Delphi程序的应用 – Photoshop色相/饱和度/明度功能》一文的BASM实用性过程,有关实现原理可参见《GDI+ 在Delphi程序的应用 -- 图像饱和度调整》和《GDI+ 在Delphi程序的应用 -- 仿Photoshop的明度调整》,纯PAS实现代码和测试例子代码见《GDI+在Delphi程序的应用 – Phot ......
1.主题与观察者
{《HeadFirst设计模式》之观察者模式 }
{ 主题与观察者 }
{ 编译工具 :Delphi7.0 }
{ 联系方式 :xshlife@163.com }
unit uWeatherReport;
interface
uses
Classes, SysUtils;
type
TObserver = class; { Forward声明,创建两个相 ......
什么是多态,字面意思就是“多种形态”,用对象来讲就是子类继承基类,而不同的子类又分别对基类进行功能的扩展。
多态在Object Pascal中是通过虚方法实现的(Virtual Method),在Object Pascal中基类的虚方法是可以被派生类覆盖(Override)的 ......
1.策略类
{《HeadFirst设计模式》之策略模式 }
{ 本单元中的类为策略类 }
{ 编译工具: Delphi7.0 }
{ E-Mail : xshlife@163.com }
unit uStrategy;
interface
type
{飞行接口,及其实现类 }
IFlyBehavior = Interface(IInterface)
procedure Fly;
......