当前位置:网站首页>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 .
边栏推荐
- Redis cache
- 旋变串判断
- [wechat applet] collaborative work and release
- Jerry's ad series MIDI function description [chapter]
- 【C语言】符号的深度理解
- Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
- Three or two things about the actual combat of OMS system
- How was MP3 born?
- TCP三次握手,四次挥手,你真的了解吗?
- torch.tensor和torch.Tensor的区别
猜你喜欢
随机推荐
数十亿公民信息遭泄漏!公有云上的数据安全还有“救”吗?
Jerry's ad series MIDI function description [chapter]
Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
maya灯建模
At the right time, the Guangzhou station of the city chain science and Technology Strategy Summit was successfully held
[buuctf.reverse] 151_ [FlareOn6]DnsChess
为什么说不变模式可以提高性能
【optimtool.unconstrain】无约束优化工具箱
【Try to Hack】宽字节注入
LambdaQueryWrapper用法
解析互联网时代的创客教育技术
Difference between ApplicationContext and beanfactory (MS)
admas零件名重复
创客思维在高等教育中的启迪作用
Redis pipeline
WGCNA分析基本教程总结
面试官:说说XSS攻击是什么?
Jerry's ad series MIDI function description [chapter]
Numpy vstack and column_ stack
PS vertical English and digital text how to change direction (vertical display)