当前位置:网站首页>类方法和类变量的使用
类方法和类变量的使用
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
- Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?
- Routing configuration and connectivity test of Huawei simulator ENSP
- maya灯建模
- Huawei ENSP simulator enables devices of multiple routers to access each other
- 华为ensp模拟器 实现多个路由器的设备可以相互访问
- 【微信小程序】协同工作与发布
- WGCNA analysis basic tutorial summary
- CAD中能显示打印不显示
- [wechat applet] collaborative work and release
猜你喜欢

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

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

华为ensp模拟器 给路由器配置DHCP

Foxit pdf editor v10.1.8 green version

shp数据制作3DTiles白膜

The video sound of station B is very low - solution

网件r7000梅林系统虚拟内存创建失败,提示USB磁盘读写速度不满足要求解决办法,有需要创建虚拟内存吗??

HWiNFO硬件检测工具v7.26绿色版

Huawei ENSP simulator configures ACL access control list

多模輸入事件分發機制詳解
随机推荐
Jerry's ad series MIDI function description [chapter]
实战模拟│JWT 登录认证
redis RDB AOF
嵌入式TC 测试用例
吐槽 B 站收费,是怪它没钱么?
解析steam教育中蕴含的众创空间
2021 CCPC 哈尔滨 B. Magical Subsequence(思维题)
华为ensp模拟器实现通信安全(交换机)
Minidom module writes and parses XML
[buuctf.reverse] 151_[FlareOn6]DnsChess
华为ensp模拟器 DNS服务器的配置
软件开发过中的采购
At the right time, the Guangzhou station of the city chain science and Technology Strategy Summit was successfully held
Le module minidom écrit et analyse XML
Why does invariant mode improve performance
Procurement in software development
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??
巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
nmap扫描