当前位置:网站首页>类方法和类变量的使用
类方法和类变量的使用
2022-07-04 20:34:00 【pcplayer】
前言
Delphi 语法,有类方法 class procedure 和 class function ;也有类变量 class var。
可以用来干嘛?
场景
WebService 服务器端,多个 SoapDataModule 共用的方法,放到一个 DataModule 里面去。
每次 SOAP 调用,需要在 SoapDataModule 实现接口方法里面,创建该 DataModule 的实例,调用其方法,然后释放。
创建和释放的代码,总是重复。并且,没有对象缓冲池。
当然,可以给这个 DataModule 实现一个基于接口释放的对象缓冲池,但代码架构就比较复杂了。
如果想少写代码,则可考虑在该 DataModule 里面实现自己的创建和释放以及缓冲管理的代码。
背景:
请查阅本篇文章的上一篇文章。
代码例子
以下代码测试通过
该模块自己实现自己的缓冲池的代码
unit UDmPool;
interface
uses
System.SysUtils, System.Classes;
type
TDmPool = class(TDataModule)
private
{ Private declarations }
class var FList: TList;
public
{ Public declarations }
class function GetMyDM: TDmPool;
class procedure ReleaseDM(DM: TDmPool);
class function GetPoolCount: Integer;
function HelloPool(const S: string): string;
end;
implementation
{%CLASSGROUP 'Vcl.Controls.TControl'}
{$R *.dfm}
{ TDmPool }
class function TDmPool.GetMyDM: TDmPool;
var
DM: TDmPool;
begin
if not Assigned(FList) then FList := TList.Create;
if FList.Count = 0 then
begin
DM := TDmPool.Create(nil);
Result := DM;
end
else
begin
DM := TDmPool(FList.Items[0]);
FList.Delete(0);
end;
end;
class function TDmPool.GetPoolCount: Integer;
begin
if not Assigned(FList) then
begin
Result := 0;
Exit;
end;
Result := FList.Count;
end;
function TDmPool.HelloPool(const S: string): string;
begin
Result := 'Hello, ' + S + '; This is DmPool';
end;
class procedure TDmPool.ReleaseDM(DM: TDmPool);
begin
FList.Add(DM);
end;
end.在 SOAPDATAMODULE 里面调用它的代码
function TSoapDM_A.HelloPool(const S: string): string;
var
DM: TDmPool;
begin
DM := TDmPool.GetMyDM;
Result := DM.HelloPool('SoapDM_A' + S);
TDmPool.ReleaseDM(DM);
end;服务器端界面显示缓冲池数量的代码
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label2.Caption := 'PoolCount = ' + TDmPool.GetPoolCount.ToString;
end;结论:
使用 class function 和 class var 可以解决把重复代码集中实现在一个类里面的预期。同时实现一个简单的缓冲池。
边栏推荐
猜你喜欢

At the right time, the Guangzhou station of the city chain science and Technology Strategy Summit was successfully held

【1200. 最小絕對差】

每日一题-LeetCode1200-最小绝对差-数组-排序

maya灯建模

Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
![Jerry added the process of turning off the touch module before turning it off [chapter]](/img/28/5e4eb39243a0c973d0b90f76571f9b.png)
Jerry added the process of turning off the touch module before turning it off [chapter]

杰理之增加进关机前把触摸模块关闭流程【篇】

数十亿公民信息遭泄漏!公有云上的数据安全还有“救”吗?

D3.js+Three.js数据可视化3d地球js特效
![[ 每周译Go ] 《How to Code in Go》系列文章上线了!!](/img/bf/77253c87bfa1512f4b8d3d8f7ebe80.png)
[ 每周译Go ] 《How to Code in Go》系列文章上线了!!
随机推荐
[1200. Différence absolue minimale]
2021 CCPC Harbin B. magical subsequence (thinking question)
B站视频 声音很小——解决办法
D3.js+Three.js数据可视化3d地球js特效
maya灯建模
In the release version, the random white screen does not display the content after opening the shutter
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
WGCNA分析基本教程总结
ApplicationContext 与 BeanFactory 区别(MS)
Minidom module writes and parses XML
学习突围3 - 关于精力
【Try to Hack】宽字节注入
Embedded TC test case
杰理之AD 系列 MIDI 功能说明【篇】
Jerry's ad series MIDI function description [chapter]
软件开发过中的采购
Render function and virtual DOM
杰理之AD 系列 MIDI 功能说明【篇】
Google colab踩坑
VIM asynchronous problem