不知有没有把IAdaptee与IAdaptor关系弄反,
概念忘记得差不多了,从哪个java例子改来的
TContainedObject是没有引用计数的,要自己处理资源问题,可以自己加入
引用计数
program Project1;
{$APPTYPE CONSOLE}
uses
Classes,SysUtils;
type
IAdaptor=interface
['{DD1D6D61-5EAF-11D7-A7B4-0050BA11326A}']
procedure WriteLine;
end;
IAdaptee=interface
['{DD1D6D62-5EAF-11D7-A7B4-0050BA11326A}']
procedure WriteTwoLine;
end;
TBase=class(TContainedObject,IAdaptor)
protected
procedure WriteLine;
public
Constructor Create;overload;
end;
TAdaptor=class(TBase,IAdaptor)
protected
procedure WriteLine;
end;
TAdaptee=class(TInterfacedObject,IAdaptee)
public
procedure WriteTwoLine;
end;
Constructor TBase.Create;
begin
inherited Create(TInterfacedObject.Create);
end;
procedure TBase.WriteLine;
begin
Writeln('-----------------------------------');
end;
procedure TAdaptor.WriteLine;
var
tmp:IAdaptee;
begin
tmp:=Controller as IAdaptee;
tmp.WriteTwoLine;
tmp:=nil;
end;
procedure TAdaptee.WriteTwoLine;
begin
Writeln('-----------------------------------');
Writeln('-----------------------------------');
end;
var
i:IAdaptor;
begin
{ 这个例子没处理资源释放 }
i:=TBase.Create;
i.WriteLine;
ReadLn;
i:=TAdaptor.Create(TAdaptee.Create);
i.WriteLine;
Readln;
end.