当前位置:网站首页>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 .
边栏推荐
- Daily question -leetcode1200- minimum absolute difference - array - sort
- 华为模拟器ensp常用命令
- 华为ensp模拟器 三层交换机
- Jerry's ad series MIDI function description [chapter]
- 刘锦程荣获2022年度中国电商行业创新人物奖
- 插入排序,选择排序,冒泡排序
- Use of redis publish subscription
- 解读创客教育中的各类智能化组织发展
- [micro service SCG] use of predict
- Huawei ENSP simulator enables devices of multiple routers to access each other
猜你喜欢

应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设

【微信小程序】协同工作与发布

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

Word文档中标题前面的黑点如何去掉

B站视频 声音很小——解决办法

Explication détaillée du mécanisme de distribution des événements d'entrée multimodes

PS vertical English and digital text how to change direction (vertical display)

每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation

OMS系统实战的三两事

华为ensp模拟器 实现多个路由器的设备可以相互访问
随机推荐
Flutter TextField示例
MP3是如何诞生的?
类方法和类变量的使用
Poster cover of glacier
杰理之AD 系列 MIDI 功能说明【篇】
Golang面试整理 三 简历如何书写
FastDfs的快速入门,三分钟带你上传下载文件到云服务器
Render function and virtual DOM
Numpy vstack and column_ stack
Le module minidom écrit et analyse XML
__ init__ () missing 2 required positive arguments
torch. Tensor and torch The difference between tensor
【C语言】符号的深度理解
Procurement in software development
y56.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(二九)
Jerry's ad series MIDI function description [chapter]
学习突围3 - 关于精力
Jerry's ad series MIDI function description [chapter]
Monitor the shuttle return button
Maya lamp modeling