TextStream.WriteLine(Line);
end;
在这些实现代码一个非常有兴趣的是:
· 装饰类的行为方法:ReadLine、WriteLine、GetEndOfText只是简单的调用相应的TextStream方法。
· SetTextStream方法在分配一个新的值前安全的释放它的方本流。
· 释造函数将TextStream := nil,因为在SetTextStream有可能释放当前的文本流。
现在可能真正的创建一个将文本转换成大写的过滤器了
接口部分的代码:
type
TUpperCaseFilter = class (TTextFilter)
public
» function ReadLine: string; override;
» procedure WriteLine(const Line: string); override;
end;
implementation
function TUpperCaseFilter.ReadLine: string;
begin
» Result := UpperCase(inherited ReadLine);
end;
procedure TUpperCaseFilter.WriteLine(const Line: string);
begin
» inherited WriteLine(UpperCase(Line));
end;
这个装饰类可以被子任何TtextStream目标使用:
function TClient.CreateOutput: TTextStream;
begin
» { 创建一个基类}
» case Destination of
» dsFile: Result := TTextFile.Create(GetFileName, fmCreate);
» dsPrinter: Result := TLinePrinter.Create;
» end;
» {判断是是否使用了装饰, 并使用同样的参数
» 重要的是我们没必要关心是装饰TTextFile ,还是TLinePrinter }
» if ConvertToUpperCase then
» Result := TUpperCaseFilter.Create(Result, True);
end;
procedure TClient.ListContents;
var
T: TTextStream;
begin
T := CreateOutput;
{ 在这里,我们并不知道是否使用了装饰}
try
{往T写内容 }
T.WriteLine(''''Contents'''');
finally
T.Free;
end;
end;
Delphi实例
正在组织。。。
上一页 [1] [2] [3] [系统软件]InstallShield Express for delphi制作安装程序定… [常用软件]InstallShield Express制作Delphi数据库安装程序 [Delphi程序]为什么选择Delphi.Net ? [Delphi程序]《关于VisiBroker For Delphi的使用》(4) [Delphi程序]Delphi 程序员代码编写标准指南 [Delphi程序]转贴:Conversion to Delphi 6: Missing unit Pro… [Delphi程序]Borland Delphi 9 的新特性 [Delphi程序]Delphi 键盘码表 [Delphi程序]Chuck Jazdzewski的离开意味着Delphi的终结吗? [Delphi程序]Delphi Access violations 问题的解决之道
|