IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

March Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...OOP   Print This     
  From the October 2015 Issue of Prestwood eMag
 
Delphi OOP:
Delphi Interfaces (IInterface, TInterfacedObject)
 
Posted 11 years ago on 11/2/2008 and updated 2/2/2009
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page

KB101439



General Info: Interface

An element of coding where you define a common set of properties and methods for use with the design of two or more classes.

Both interfaces and abstract classes are types of abstraction. With interfaces, like abstract classes, you cannot provide any implementation. However, unlike abstract classes, interfaces are not based on inheritance. You can apply an Interface to any class in your class tree. In a real sense, interfaces are a technique for designing horizontally in a class hierarchy (as opposed to inheritance where you design vertically). Using interfaces in your class design allows your system to evolve without breaking existing code.

Delphi Interfaces

In Delphi, you use interfaces for both com objects and language interfaces and make use of IUnknown, IInterface, and/or TInterfacedObject.

For a pure language interface, add your specified proprieties, procedures, and functions to an interface that descends from IInterface (the base interface) as an interface, no implementation. Then have your implementing class inherit from TInterfacedObject and implement the interface.

For extending the VCL, you descend from the class you wish to extend, then implement an interface from IInterface and add the required functions QueryInterface, _AddRef, and _Release methods (refer to TInterfacedObject for an example).

For a com object, you descend from IUnknown. Descending from IUnknown instead of IInterface informs the Delphi compiler that the interface must be compatible with COM objects -- a Windows feature).

When defining an interface, define it in the type block just like you do for a class but you use the interface keyword instead of the class keyword and in the interfaces section only. Since interfaces, by definition, do not have any implementation details, all you do is specify it in the type block. Then implement in all classes that support the interface.

Syntax Example:
//Language interface:
//Interface section of unit.
IHuman = Interface(IInterface)
  //Specify interface methods and properties here.

end;
  
TCyborg = class(TInterfacedObject)
end;
  
TCyborgHuman = class(TCyborg, IHuman)
//Specify each here and implement in
//implementation section.
end;

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).

More Info

Definition:  Interface

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1182A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101439 Counter
23712
Since 11/2/2008
-
   Contact Us!
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


Go ahead!   Use Us! Call: 916-726-5675 


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]