达耘 發表於 2023-10-3 11:09:00

delphi泛型模板编程

<p>delphi泛型模板编程</p>
<p>泛型模板编程的关键:泛型作用体现在模板,体现在虚实之间相互转换。以虚御实,以实立虚,虚中有实,实中有虚,虚事实做,实事虚做。框架、流程。。无不如此,编程之道。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">unit TxInfo;

interface

uses
System.Types,
System.Classes,
System.SysUtils,
Generics.Collections;

type
TPeople = record
    Name: string;
    Age: string;
end;

type
TTxInfo&lt;T&gt; = class
public
    PeopleList: TList&lt;T&gt;;
public
    constructor Create;
    destructor Destroy; override;
private
    function ProcessInfo(aAPeopleStr: string): T; virtual; abstract;
public
    procedure GetInfo(AStr: string); overload;
end;

type
TMyNetInfo&lt;T&gt; = class(TTxInfo&lt;T&gt;)//泛型类可以继承
public
    function ProcessInfo(APeopleStr: string): T; override;
end;

implementation

{ TTxHttp }
constructor TTxInfo&lt;T&gt;.Create;
begin
inherited Create();
PeopleList := TList&lt;T&gt;.Create;
end;

destructor TTxInfo&lt;T&gt;.Destroy;
begin
PeopleList.Free;
inherited Destroy;
end;

procedure TTxInfo&lt;T&gt;.GetInfo(AStr: string);
var
m_Info: T;
begin
PeopleList.Clear;
m_Info := ProcessInfo(AStr);
PeopleList.Add(m_Info);
end;

{ TMyNetInfo&lt;TFilmInfoT&gt; }
function TMyNetInfo&lt;T&gt;.ProcessInfo(APeopleStr: string): T;
begin
var pepole: TPeople;
pepole.Name:= APeopleStr;
Result:=T(Pointer(@pepole)^);//实转虚
end;

end.
</pre>
</div>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">procedure TPool&lt;T&gt;.Unlock(Value: T);
begin
FCS.Enter;
try
    if TComponent(Addr(Value)).Tag = DynCreate then //虚转实
      Value.free
    else
    begin
      FList.Add(Value);
    end;
finally
    FCS.Leave;
end;
end;
</pre>
</div>
<p>  </p>
<p> 泛型模板编程</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">//cxg 2023-10-3

unit pool;

interface

uses
System.Generics.Collections, Classes, SyncObjs, SysUtils, DateUtils;

const
DynCreate = 5;

type
TPool&lt;T: class&gt; = class
private
    FCS: TCriticalSection;
    FList: TList&lt;T&gt;;
    FPoolSize: Integer;
public
    constructor Create; virtual;
    destructor Destroy; override;
protected
    procedure Init; virtual;
    function NewObj(owner: TComponent = nil): T; virtual; abstract;
public
    function Lock: T;
    procedure Unlock(Value: T);
end;

implementation

{ TPool&lt;T&gt; }

constructor TPool&lt;T&gt;.Create;
begin
inherited;
FList := TList&lt;T&gt;.create;
FCS := TCriticalSection.Create;
end;

destructor TPool&lt;T&gt;.Destroy;
begin
FList.Clear;
FreeAndNil(FList);
FreeAndNil(FCS);
inherited;
end;

procedure TPool&lt;T&gt;.Init;
begin
while FList.Count &lt; Self.FPoolSize do
    FList.Add(NewObj(nil));
end;

function TPool&lt;T&gt;.Lock: T;
begin
FCS.Enter;
try
    if FList.Count &gt; 0 then
    begin
      Result := FList.Last;
      FList.Delete(FList.Count - 1);
    end
    else
    begin
      Result := NewObj;
      TComponent(TypeInfo(Result)).Tag := DynCreate;
    end;
finally
    FCS.Leave;
end;
end;

procedure TPool&lt;T&gt;.Unlock(Value: T);
begin
FCS.Enter;
try
    if TComponent(TypeInfo(Value)).Tag = DynCreate then
      Value.free
    else
    begin
      FList.Add(Value);
    end;
finally
    FCS.Leave;
end;
end;

end.
</pre>
</div>
<p>  </p>
<p>  </p>
<p>  </p>

</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/17740909.html</p><br><br>
来源:https://www.cnblogs.com/hnxxcxg/p/17740909.html
頁: [1]
查看完整版本: delphi泛型模板编程