当前位置:网站首页>Use of class methods and class variables
Use of class methods and class variables
2022-07-04 21:34:00 【pcplayer】
Preface
Delphi grammar , Class method class procedure and class function ; There are also class variables class var.
What can be used for ?
scene
WebService Server side , Multiple SoapDataModule Common method , In a DataModule Go inside .
Every time SOAP call , Need to be in SoapDataModule Implement interface methods , Create the DataModule Example , Call its methods , Then release .
Create and release code , Always repeat . also , No object buffer pool .
Of course , You can give this DataModule Implement an object buffer pool based on interface release , But the code architecture is more complex .
If you want to write less code , You can consider in this DataModule It implements its own creation, release and buffer management code .
background :
Please refer to the previous article of this article .
Code example
The following code passed the test
The module implements its own buffer pool code
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.stay SOAPDATAMODULE Inside the code that calls it
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;The server-side interface shows the code of the number of buffer pools
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label2.Caption := 'PoolCount = ' + TDmPool.GetPoolCount.ToString;
end;Conclusion :
Use class function and class var It can solve the expectation of implementing repeated code in one class . At the same time, implement a simple buffer pool .
边栏推荐
- Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
- 旋变串判断
- 杰理之AD 系列 MIDI 功能说明【篇】
- numpy vstack 和 column_stack
- Flutter 返回按钮的监听
- 每日一题-LeetCode1200-最小绝对差-数组-排序
- Jerry's ad series MIDI function description [chapter]
- Three or two things about the actual combat of OMS system
- Redis bloom filter
- FastDfs的快速入门,三分钟带你上传下载文件到云服务器
猜你喜欢

MP3是如何诞生的?

FastDfs的快速入门,三分钟带你上传下载文件到云服务器

y56.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(二九)

Difference between ApplicationContext and beanfactory (MS)
![Jerry's ad series MIDI function description [chapter]](/img/d7/348d85eb9f69ffd75612eeba56b16e.png)
Jerry's ad series MIDI function description [chapter]

Y56. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (29)

NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??

如何使用ConcurrentLinkedQueue做一个缓存队列

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

搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
随机推荐
2021 CCPC Harbin I. power and zero (binary + thinking)
Jerry's ad series MIDI function description [chapter]
__ init__ () missing 2 required positive arguments
Go语言循环语句(第10课中3)
redis事务
Roast B station charges, is it because it has no money?
MYSQL 用!=查询不出等于null的数据,解决办法
Why does invariant mode improve performance
Shutter WebView example
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
y56.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(二九)
解读创客教育中的各类智能化组织发展
Google colab踩坑
每日一题-LeetCode1200-最小绝对差-数组-排序
杰理之AD 系列 MIDI 功能说明【篇】
Analysis of maker education technology in the Internet Era
minidom 模块写入和解析 XML
Flutter在 release版本,打开后随机白屏不显示内容
杰理之AD 系列 MIDI 功能说明【篇】
解析steam教育中蕴含的众创空间