- Mastering Delphi Programming:A Complete Reference Guide
- Primo? Gabrijel?i?
- 249字
- 2021-06-24 12:33:36
Interfaces
From the viewpoint of memory management, interfaces in Delphi are implemented as classes with added reference counting. To create an interface, you actually have to create an object of a class, which will get a reference count of 1. If you then assign this interface to another variable, both will point to the same memory and the reference count will be incremented to 2.
There is no equivalent to SetLength or UniqueString that would make a unique copy of an interface. That would require duplicating the underlying object and Delphi has no built-in support for that.
The object implementing the interface is destroyed when its reference count falls to 0:
var
i1, i2: IInterface;
begin
i1 := TInterfacedObject.Create;
// i1 points to an object with reference count 1
i2 := i1;
// both i1 and i2 point to a same object with reference count 2
i1 := nil;
// i2 now points to an object with reference count 1
i2 := nil;
// reference count dropped to 0 and object is destroyed
end;
Although interfaces are very similar to classes, all this reference count management takes its cost. It is implemented with something called interlocked instructions which are a bit slower than normal increment/decrement instructions. I'll discuss this in more detail in Chapter 5, Getting Started with the Parallel World.
This only makes a measurable difference when you are assigning interfaces a lot, but sometimes this is exactly what happens. I'll show an example in the next section.
- Istio入門與實戰
- 基于Proteus和Keil的C51程序設計項目教程(第2版):理論、仿真、實踐相融合
- 電腦組裝、維護、維修全能一本通(全彩版)
- INSTANT ForgedUI Starter
- 從零開始學51單片機C語言
- 微服務分布式架構基礎與實戰:基于Spring Boot + Spring Cloud
- STM32嵌入式技術應用開發全案例實踐
- 計算機組裝維修與外設配置(高等職業院校教改示范教材·計算機系列)
- 龍芯自主可信計算及應用
- VMware Workstation:No Experience Necessary
- “硬”核:硬件產品成功密碼
- Hands-On Deep Learning for Images with TensorFlow
- Spring Security 3.x Cookbook
- Mastering Machine Learning on AWS
- 筆記本電腦維修技能實訓