《GOF设计模式》—职责链(CHAIN OF RESPONSIBILITY)—Delphi源码示例:表示请求(使用处理函数)
2010-12-25 13:19
736 查看
示例:处理函数
说明:
另一选择是使用一个处理函数,这个函数以一个请求码(如一个整型常数或一个字符串)为参数。这种方法支持请求数目不限。唯一的要求是发送方和接受方在请求如何编码问题上应达成一致。
这种方法更为灵活,但它需要用条件语句来区分请求代码以分派请求。另外,无法用类型安全的方法来传递请求参数(或参数组,如“a-1,b-2”),因此它们必须被手工打包和解包。显然,相对于直接调用一个操作来说它不太安全。
代码:
unit uChainOfResponsibility1;
interface
uses
Dialogs;
type
THandler = class
private
FSuccessor: THandler;
public
constructor Create(const ASuccessor: THandler = nil);
//---
procedure HandleRequest(const ACode: string); virtual;
end;
TConcreteHandler1 = class(THandler)
public
procedure HandleRequest(const ACode: string); override;
end;
TConcreteHandler2 = class(THandler)
public
procedure HandleRequest(const ACode: string); override;
end;
implementation
constructor THandler.Create(const ASuccessor: THandler = nil);
begin
FSuccessor := ASuccessor;
end;
procedure THandler.HandleRequest(const ACode: string);
begin
case ACode[1] of
'A':
begin
if Length(ACode) > 1 then
begin
case ACode[2] of
'1'..'9': ShowMessage('An');
else
ShowMessage('Ax');
end;
end
else
ShowMessage('Ax');
end;
'B':
begin
ShowMessage('Bx');
end;
else
if assigned(FSuccessor) then
FSuccessor.HandleRequest(ACode);
end;
end;
procedure TConcreteHandler1.HandleRequest(const ACode: string);
begin
case ACode[1] of
'C': ShowMessage('Cx');
else
inherited;
end;
end;
procedure TConcreteHandler2.HandleRequest;
begin
case ACode[1] of
'D': ShowMessage('Dx');
else
inherited;
end;
end;
end.
procedure TForm1.Button1Click(Sender: TObject);
var
AHandler1: TConcreteHandler1;
AHandler2: TConcreteHandler2;
begin
AHandler2 := TConcreteHandler2.Create;
AHandler1 := TConcreteHandler1.Create(AHandler2);
try
AHandler1.HandleRequest('A1');
finally
AHandler1.Free;
AHandler2.Free;
end;
end;
说明:
另一选择是使用一个处理函数,这个函数以一个请求码(如一个整型常数或一个字符串)为参数。这种方法支持请求数目不限。唯一的要求是发送方和接受方在请求如何编码问题上应达成一致。
这种方法更为灵活,但它需要用条件语句来区分请求代码以分派请求。另外,无法用类型安全的方法来传递请求参数(或参数组,如“a-1,b-2”),因此它们必须被手工打包和解包。显然,相对于直接调用一个操作来说它不太安全。
代码:
unit uChainOfResponsibility1;
interface
uses
Dialogs;
type
THandler = class
private
FSuccessor: THandler;
public
constructor Create(const ASuccessor: THandler = nil);
//---
procedure HandleRequest(const ACode: string); virtual;
end;
TConcreteHandler1 = class(THandler)
public
procedure HandleRequest(const ACode: string); override;
end;
TConcreteHandler2 = class(THandler)
public
procedure HandleRequest(const ACode: string); override;
end;
implementation
constructor THandler.Create(const ASuccessor: THandler = nil);
begin
FSuccessor := ASuccessor;
end;
procedure THandler.HandleRequest(const ACode: string);
begin
case ACode[1] of
'A':
begin
if Length(ACode) > 1 then
begin
case ACode[2] of
'1'..'9': ShowMessage('An');
else
ShowMessage('Ax');
end;
end
else
ShowMessage('Ax');
end;
'B':
begin
ShowMessage('Bx');
end;
else
if assigned(FSuccessor) then
FSuccessor.HandleRequest(ACode);
end;
end;
procedure TConcreteHandler1.HandleRequest(const ACode: string);
begin
case ACode[1] of
'C': ShowMessage('Cx');
else
inherited;
end;
end;
procedure TConcreteHandler2.HandleRequest;
begin
case ACode[1] of
'D': ShowMessage('Dx');
else
inherited;
end;
end;
end.
procedure TForm1.Button1Click(Sender: TObject);
var
AHandler1: TConcreteHandler1;
AHandler2: TConcreteHandler2;
begin
AHandler2 := TConcreteHandler2.Create;
AHandler1 := TConcreteHandler1.Create(AHandler2);
try
AHandler1.HandleRequest('A1');
finally
AHandler1.Free;
AHandler2.Free;
end;
end;
相关文章推荐
- 《GOF设计模式》—职责链(CHAIN OF RESPONSIBILITY)—Delphi源码示例:表示请求(使用请求对象)
- 《GOF设计模式》—职责链(CHAIN OF RESPONSIBILITY)—Delphi源码示例:职责链接口
- 《GOF设计模式》—职责链(CHAIN OF RESPONSIBILITY)—Delphi源码示例:帮助机制
- 《GOF设计模式》—职责链(CHAIN OF RESPONSIBILITY)—Delphi源码示例:doesNotUnderstand机制
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:单件接口(使用类字段)
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:创建Singleton类的子类(使用环境变量)
- 《GOF设计模式》—解释器 (INTERPRETER)—Delphi源码示例:使用Flyweight模式共享终结符
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于类的原型管理器(使用TList实现)
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于类的原型管理器(使用TStringList实现)
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:创建Singleton类的子类(使用注册表)
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于类的原型管理器(使用TList和TObject对象服务实现)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(使用抽象操作)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(使用抽象操作)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(使用代理对象)
- 《GOF设计模式》—命令(COMMAND)—Delphi源码示例:参数化机制(使用命令模式)
- 《GOF设计模式》—代理(PROXY)—Delphi源码示例:文档编辑器(使用虚代理实现)
- 《GOF设计模式》—命令(COMMAND)—Delphi源码示例:在不同的时刻指定、排列和执行请求
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(使用代理对象)
- 《GOF设计模式》—代理(PROXY)—Delphi源码示例:文档编辑器(使用doesNotUnderstand的Proxy)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:使用双向适配器提供透明操作