Now let's dive deeper...
Base IInterface Class
The base interface class in the VCL is IInterface. When defining an interface, you can specify IInterface, leave it out, or specify an already defined interface.
Declare from base interface class:
ITrainedDog = Interface
end;
Or you can specify the base interface class:
ITrainedDog = Interface(IInterface)
end;
Or another existing interface:
ITrainedByMike = Interface(ITrainedDog)
end;
Class Declarations With Interface Implementations
You specify the interface you wish to implement as part of your class declaration comma separated after the parent class.
The following example shows a single T1 class descending from TInterfacedObject so I didn't have to implement the QueryInterface, _AddRef, and _Release functions.
T1 = Class(TInterfacedObject)
End
The following example shows a single class that implements one interface. This new class inherits from T1 (a cyborg class) but acts like a human.
T800Human = Class(T1, IHuman)
End
The following example shows a single class that implements three interfaces. This new class inherits from T1 (a cyborg class) but this cyborg acts like a very well trained dog.
T800Dog = Class(T1, ITrainedDog, IShowDog, IGuardDog)
End
Delphi 2009 Working Example of a Language Interface
The following example demonstrates implementing a very simple language interface. The interface is named IHuman which includes one property and one method. Our resulting class is named TCyborgHuman and, for clarity, our TCyborgHuman class also inherits from a class called TCyborg which for ease of implementation descends from TInterfacedObject (again, so I didn't have to implement the QueryInterface, _AddRef, and _Release functions).
Create a form and place a button on it. Then add a unit called CyborgUnit.pas as follows:
unit CyborgUnit;
interface
uses
Dialogs;
type
IHuman = Interface(IInterface)
//These methods are required to support our property.
function GetHumanName: String;
procedure SetHumanName(const Value: String);
//Defined properties and methods for interface.
property HumanName: String read GetHumanName write SetHumanName;
procedure Speak(pSentence: String);
end;
TCyborg = class(TInterfacedObject)
end;
TCyborgHuman = class(TCyborg, IHuman)
private
FHumanName: String;
protected
function GetHumanName: String;
procedure SetHumanName(const Value: String);
public
property HumanName: String read GetHumanName write SetHumanName;
procedure Speak(pSentence: String);
end;
implementation
function TCyborgHuman.GetHumanName: String;
begin
Result := FHumanName;
end;
procedure TCyborgHuman.SetHumanName(const Value: String);
begin
FHumanName := Value;
end;
procedure TCyborgHuman.Speak(pSentence: String);
begin
//We had to add the Dialogs unit to this unit
//because we are using ShowMessage.
ShowMessage(pSentence);
end;
end.
Now let's use our new CyborgHuman class which supports the IHuman interface. Alter the code of your button on your form as follows:
procedure TForm1.Button1Click(Sender: TObject);
var
MyRobot: TCyborgHuman;
begin
MyRobot := TCyborgHuman.Create;
MyRobot.HumanName := 'Nicole';
MyRobot.Speak('Hi, my name is ' + MyRobot.HumanName + '.');
end;
Com Interfaces
The scope of this article was on overview material and implementing a language interface. There are plenty of good examples on the internet for implementing a com interface. The main points of implementing a Windows com object are:
- Your interface descends from IUnknown which is the fundamental interface for com objects that indicates you must support QueryInterface, AddRef, and Release.
- You should use a GUID in the interface declaration to uniquely identify it (Ctrl+Shift+G to insert into code).