http://sergworks.wordpress.com/2011/12/08/why-we-need-interfaces-in-delphi/

Why we need interfaces in Delphi.

Posted on December 8, 2011 by Serg

Objects are normally accessed by an object reference.

Interface reference is a different method to access an object’s functionality.

A simple question – why do we need interface references at all, why can’t we use object references everywhere?

There are several reasons to use interface references instead of object references,

but most important of them (at least historically) is accessing an object created in a different program module.

Let us consider a simple example – an object is created in .dll module and consumed in .exe module.

The TMathObject class implements Square and Cube functions on the FOperandfield;

we start with the following naive code:

unit MathUnit;

interface

type
TMathObject = class
private
FOperand: Double;
public
function Square: Double;
function Cube: Double;
property Operand: Double read FOperand write FOperand;
end; implementation function TMathObject.Square: Double;
begin
Result:= Sqr(FOperand);
end; function TMathObject.Cube: Double;
begin
Result:= Sqr(FOperand) * FOperand;
end; end.

We want to create and destroy TMathObject instances in dll module:

library MathDll;

uses
MathUnit in 'MathUnit.pas'; function CreateObject: TMathObject;
begin
Result:= TMathObject.Create;
end; procedure FreeObject(Obj: TMathObject);
begin
Obj.Free;
end; exports
CreateObject, FreeObject; {$R *.res} begin
end.

and use an instance of TMathObject in exe module:

program MathTest;

{$APPTYPE CONSOLE}

uses
MathUnit in 'MathUnit.pas'; function CreateObject: TMathObject; external 'MathDll.dll';
procedure FreeObject(Obj: TMathObject); external 'MathDll.dll'; var
MathObj: TMathObject; begin
MathObj:= CreateObject;
MathObj.Operand:= ;
Writeln('Square = ', MathObj.Square::, '; Cube = ', MathObj.Cube::);
FreeObject(MathObj);
Write('Press ''Enter'' key ... ');
Readln;
end.

If you compile the above example you can see it works, but TMathObject implementation (MathUnit.pas) is duplicated

in both program modules (MathTest.exe and MathDll.dll), and that is not just a waste of program memory.

One of the main reasons to split a program into program modules is a possibility to modify the modules separately;

for example to modify and deploy a different .dll version while keeping an .exe module intact.

In the above example the implementation of TMathObject is a contract that both sides (exe and dll) should adhere,

so the implementation of TMathObject can’t be changed in dll module only.

We need a different form of contract that does not include an object’s implementation.

A possible solution is to introduce a base class containing virtual abstract methods only:

unit BaseMath;

interface

type
TBaseMathObject = class
protected
function GetOperand: Double; virtual; abstract;
procedure SetOperand(const Value: Double); virtual; abstract;
public
function Square: Double; virtual; abstract;
function Cube: Double; virtual; abstract;
property Operand: Double read GetOperand write SetOperand;
end; implementation end.
Note that we can’t access FOperand field directly now because it is a part of TMathObject implementation that should be hidden in .dll module,
so we introduce getter (GetOperand) and setter (SetOperand) virtual methods.
Now we inherit a class that implements virtual methods from TBaseMathObject.
unit MathUnit;

interface

uses BaseMath;

type
TMathObject = class(TBaseMathObject)
private
FOperand: Double;
protected
function GetOperand: Double; override;
procedure SetOperand(const Value: Double); override;
public
function Square: Double; override;
function Cube: Double; override;
end; implementation function TMathObject.GetOperand: Double;
begin
Result:= FOperand;
end; procedure TMathObject.SetOperand(const Value: Double);
begin
FOperand:= Value;
end; function TMathObject.Square: Double;
begin
Result:= Sqr(FOperand);
end; function TMathObject.Cube: Double;
begin
Result:= Sqr(FOperand) * FOperand;
end; end.

The library module source code now is

library MathDll;

uses
BaseMath in 'BaseMath.pas',
MathUnit in 'MathUnit.pas'; function CreateObject: TBaseMathObject;
begin
Result:= TMathObject.Create;
end; procedure FreeObject(Obj: TBaseMathObject);
begin
Obj.Free;
end; exports
CreateObject, FreeObject; {$R *.res} begin
end.
The executable module source code is
program MathTest;

{$APPTYPE CONSOLE}

uses
BaseMath in 'BaseMath.pas'; function CreateObject: TBaseMathObject; external 'MathDll.dll';
procedure FreeObject(Obj: TBaseMathObject); external 'MathDll.dll'; var
MathObj: TBaseMathObject; begin
MathObj:= CreateObject;
MathObj.Operand:= ;
Writeln('Square = ', MathObj.Square::, '; Cube = ', MathObj.Cube::);
FreeObject(MathObj);
Write('Press ''Enter'' key ... ');
Readln;
end.

We can see that MathTest project does not contain MathUnit.pas unit, and is not dependent on TMathObject implementation;

in fact MathTest project does not know that TMathObject class even exist.

We can change TMathObjectimplementation in dll module as much as we want provided that we keepTBaseMathObject intact,

inherit TMathObject from TBaseMathObject and override TBaseMathObject‘s virtual abstract methods.

We implemented a general concept of interface in the form of pure abstract class.

Pure abstract classes are a way how interfaces are implemented in C++ .

This approach has a limited value in Delphi because Delphi does not support multiple inheritance,

and a Delphi class can have only one contract in the form of base abstract class.

Another problem is a limited use of ‘is’ and ‘as’ operators for an object created in a different program module:

Starting from version 3 Delphi introduces a concept of interface that is different from a pure abstract class

and solves the problems with object’s export by using interface references instead of object references:

unit BaseMath;

interface

type
IBaseMath = interface
['{92E9AFF4-25B7-41BD-9EB6-557D12F98BE6}']
function GetOperand: Double;
procedure SetOperand(const Value: Double);
function Square: Double;
function Cube: Double;
property Operand: Double read GetOperand write SetOperand;
end; implementation end.

There is no need to inherit TMathObject class from a given base class now;

we can inherit TMathObject class from any class we like.

Since all Delphi interfaces are descendants of IUnknown (also nicknamed as IInterface in Delphi)

we should also implement the methods of IUnknown interface in TMathObject class.

Delphi provides a helper TInterfacedObject class that already implements the methods of IUnknown

and can be used as TMathObject ancestor:

unit MathUnit;

interface

uses BaseMath;

type
TMathObject = class(TInterfacedObject, IBaseMath)
private
FOperand: Double;
protected
function GetOperand: Double;
procedure SetOperand(const Value: Double);
public
function Square: Double;
function Cube: Double;
end; implementation function TMathObject.GetOperand: Double;
begin
Result:= FOperand;
end; procedure TMathObject.SetOperand(const Value: Double);
begin
FOperand:= Value;
end; function TMathObject.Square: Double;
begin
Result:= Sqr(FOperand);
end; function TMathObject.Cube: Double;
begin
Result:= Sqr(FOperand) * FOperand;
end; end.

There is no need for FreeObject procedure now.

The FreeObject procedure was introduced in the previous examples

to enforce that a TMathObject instance is destroyed

in the same program module where it was created (i.e. in .dll module).

It is always a good rule of thumb that the one who creates an object is the one who destroys it.

But now there is no need to enforce it – if we use interface references object instances

are automatically destroyed in the same program module where they were created.

library MathDll;

uses
BaseMath in 'BaseMath.pas',
MathUnit in 'MathUnit.pas'; function CreateObject: IBaseMath;
begin
Result:= TMathObject.Create;
end; exports
CreateObject; {$R *.res} begin
end.

In the next example a TMathObject object instance is destroyed by assigning nil value to MathObj interface reference.

In most cases there is no need for doing it because an object is

destroyed automatically when all interface references goes out of scope.

In the following code the MathObj interface reference is a global variable and never goes out of scope,

so assigning it to nil explicitly makes sense:

program MathTest;

{$APPTYPE CONSOLE}

uses
BaseMath in 'BaseMath.pas'; function CreateObject: IBaseMath; external 'MathDll.dll'; var
MathObj: IBaseMath; begin
MathObj:= CreateObject;
MathObj.Operand:= ;
Writeln('Square = ', MathObj.Square::, '; Cube = ', MathObj.Cube::);
MathObj:= nil;
Write('Press ''Enter'' key ... ');
Readln;
end.

The interfaced demo works fine, but when I try to load/unload the DLL dinamically,

an access violation is raised at the end of the program in the System unit at the _IntfClear function.

program MathTest;

{$APPTYPE CONSOLE}

uses
Windows,
BaseMath in ‘BaseMath.pas'; //function CreateObject: IBaseMath; external ‘MathDll.dll'; type TCreateObject = function: IBaseMath; stdcall; var CreateObject: TCreateObject;
MathObj: IBaseMath;
Dll: THandle; begin
Dll := LoadLibrary(‘MathDll.dll’);
CreateObject := GetProcAddress(Dll, ‘CreateObject’); MathObj:= CreateObject;
MathObj.Operand:= ;
Writeln(‘Square = ‘, MathObj.Square::, ‘; Cube = ‘, MathObj.Cube::);
MathObj:= nil; FreeLibrary(Dll); Write(‘Press ”Enter” key … ‘);
Readln;
// Access Violation is raised here
end.

Sorry. It was a calling convention problem.
TCreateObject = function: IBaseMath;
works fine.

Great articles.

Great Approach of explanation.

I read a lot about interfaces, but this articles make you understand EXACTLY what is an Interface.

The three examples is the best way to progressively move from one concept to the other.

Thank you very much for your efforts.

Why we need interfaces in Delphi的更多相关文章

  1. Delphi Interfaces

    http://www.delphibasics.co.uk/Article.asp?Name=Interface The reason for interfaces   Classes that ex ...

  2. [Delphi] Delphi版本号对照

    VER300    Delphi Seattle / C++Builder Seattle    23    230    (Delphi:Win32/Win64/OSX/iOS32/iOS64/An ...

  3. Delphi的分配及释放---New/Dispose, GetMem/FreeMem及其它函数的区别与相同

    转载自:http://www.cnblogs.com/qiusl/p/4028437.html?utm_source=tuicool 我估摸着内存分配+释放是个基础的函数,有些人可能没注意此类函数或细 ...

  4. Delphi开发的IP地址修改工具

    用Delphi进行开发的,直接修改注册表,需重启电脑后才生效

  5. Delphi XE5教程3:实例程序

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  6. delphi 保存网页MHT

    delphi 保存网页MHT   uses ADODB_TLB, CDO_TLB, ComObj,MSHTML;{$R *.dfm}{能把网页如 WWW.QQ.COM保存为一个单文件 .MHT但不能把 ...

  7. Delphi GDI+ Library

    GDI+ LibraryThis library enables GDI+ functionality for Delphi 2009 and later. It differs from other ...

  8. Delphi实现HTMLWebBrowser实现HTML界面

    HTML的界面有以下特点:图文混排,格式灵活,可以包含Flash.声音和视频等,实现图文声像的多媒体界面,而且易于建立和维护.另外,HTML的显示环境一般机器上都具备,通常不需要安装额外的软件.当然, ...

  9. [转]Delphi I/O Errors

    The following are the Windows API (and former DOS) IO errors, which are also the IO errors often ret ...

随机推荐

  1. 百度地图Api之自定义标注:(获得标注的经纬度和中心经纬度即缩放度)

    百度地图Api之自定义标注:(获得标注的经纬度和中心经纬度即缩放度) <%@ Page Language="C#" AutoEventWireup="true&qu ...

  2. Linux makefile教程之概述一[转]

    概述—— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些 Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makef ...

  3. hdu 1115(计算多边形重心)

    题意:已知一多边形没有边相交,质量分布均匀.顺序给出多边形的顶点坐标,求其重心. 分析: 求多边形重心的题目大致有这么几种: 1,质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心 X ...

  4. Loadrunner中与事务相关的概念及函数

    一.事务 事务是指用户在客户端做一种或多种业务所需要的操作集,通过事务函数可以标记完成该业务所需要的操作内容:另一方面可以用来统计用户操作的相应时间.事务响应时间是指通过记录用户请求的开始时间和服务器 ...

  5. 代码以兼容高亮方式发布.xml

    函数名: abort  功  能: 异常终止一个进程  用  法: void abort(void);  程序例:  #include <stdio.h>  #include <st ...

  6. layout相关

    大致看了看布局大致有5种(approximately) 1. LinearLayout 2. RelativeLayout 3. FrameLayout 4. TableLayout 5. GridL ...

  7. 高薪诚聘.NET MVC开发工程师

    你想有大好的发展前途吗?你想拥有高的月薪吗? 赶快来吧! 1.企业网站.电子商务开发: 2.进行详细设计.代码开发,配合测试,高质量完成项目: 3.参与技术难题攻关.组织技术积累等工作. 任职资格: ...

  8. 渗透测试实例Windows XP SP2

    一.msf> use exploit/windows/dcerpc/ms03_026_dcom.看到命令提示符的改变表明该命令已经运行成功. 二.为漏洞利用代码设置必要的参数,show opti ...

  9. python install 2.7.10

    CentOS 6.5升级Python和安装IPython 后来换成了CentOS 6.5,系统自带的Python版本是2.6.6. 图一:安装IPython需求 已经安装好gcc等编译工具.系统自带P ...

  10. 30个有关Python的小技巧

    从我开始学习python的时候,我就开始自己总结一个python小技巧的集合.后来当我什么时候在Stack Overflow或者在某个开源软件里看到一段很酷代码的时候,我就很惊讶:原来还能这么做!,当 ...