当前位置:网站首页>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 .
边栏推荐
- 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??
- [public class preview]: basis and practice of video quality evaluation
- 为什么说不变模式可以提高性能
- Test case (TC)
- Go语言循环语句(第10课中3)
- Why does invariant mode improve performance
- How was MP3 born?
- Redis transaction
- redis03——Redis的网络配置与心跳机制
- [buuctf.reverse] 151_[FlareOn6]DnsChess
猜你喜欢
随机推荐
奋斗正当时,城链科技战略峰会广州站圆满召开
redis03——Redis的网络配置与心跳机制
Learning breakout 3 - about energy
Maidong Internet won the bid of Beijing life insurance
UTF encoding and character set in golang
宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
Huawei simulator ENSP common commands
杰理之AD 系列 MIDI 功能说明【篇】
改善机器视觉系统的方法
【活动早知道】LiveVideoStack近期活动一览
Methods of improving machine vision system
Numpy vstack and column_ stack
华为ensp模拟器实现通信安全(交换机)
Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system
【C語言】符號的深度理解
解读创客教育中的各类智能化组织发展
插入排序,选择排序,冒泡排序
Google colab踩坑
redis发布订阅的使用
EhLib 数据库记录的下拉选择
![[C language] deep understanding of symbols](/img/4b/26cf10baa29eeff08101dcbbb673a2.png)








