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
相关文档:
在一个单元中声明的多个类互为友元类
type
TMyClass = class
GUID: string;
Name: string;
bSex: Boolean;
Tel : string;
end;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedu ......
1.主题与观察者
{《HeadFirst设计模式》之观察者模式 }
{ 主题与观察者 }
{ 编译工具 :Delphi7.0 }
{ 联系方式 :xshlife@163.com }
unit uWeatherReport;
interface
uses
Classes, SysUtils;
type
TObserver = class; { Forward声明,创建两个相 ......
继承是为了表现类与类之间“是一种”关系,是多态存在的基础,继承是面象对象必不可少的基础,只支持封装而不支持继承的语言只能称为“基于对象”(Object-Based)面非面向对象“Object-Oriented”;
Object Pascal只支持单继承,也就是一个派生类只能有一个基类
但可以实现多个接口 ......
容器的主要职责有两个:存放元素和浏览元素。根据单一职责原则(SRP)要将二者分开,于是将浏览功能打包封装就有了迭代器。
用迭代器封装对动态数组的遍历:
1.容器中的元素类
{《HeadFirst设计模式》之迭代器模式 }
{ 容器中的元素类 ......
1.被装饰者
{《HeadFirst设计模式》之装饰模式 }
{ 本单元中的类为被装饰者 }
{ 编译工具: Delphi7.0 }
{ E-Mail : xshlife@163.com }
unit uComponent;
interface
type
TBeverage = class(TObject) //抽象饮料类
protected
FDescription: String;
public
......