Difference between revisions of "How To Use Interfaces"

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
 
Copy the text below and it will demonstrate how to use [[Interfaces]] to write less code, avoid code repetition,..., this is a fully working program.
 
Copy the text below and it will demonstrate how to use [[Interfaces]] to write less code, avoid code repetition,..., this is a fully working program.
 
+
<syntaxhighlight>{$mode objfpc}{$H+}{$interfaces com}
>>this is a fully working program
 
 
 
No, this code contains memory leaks and incorrect use of interfaces. Please pay attention to this
 
 
 
<syntaxhighlight lang="pascal">
 
program interfacesygenerics;
 
 
 
{$mode objfpc}{$H+}
 
 
 
const
 
  STestInterface = '{3FB19775-F5FA-464C-B10C-D8137D742088}';
 
 
 
 
type
 
type
 
   ITestInterface = interface
 
   ITestInterface = interface
     [STestInterface]
+
     ['{3FB19775-F5FA-464C-B10C-D8137D742088}']
 
     procedure DoSomething;
 
     procedure DoSomething;
 
     procedure DoItAll;
 
     procedure DoItAll;
 
   end;
 
   end;
  
  { Base class Start }
+
   TBaseObject = class(TInterfacedObject,ITestInterface)
   TBaseObject = class(TInterfacedObject)
 
    procedure DoAll;
 
  end;
 
 
 
  procedure TBaseObject.DoAll;
 
  begin
 
    Writeln('TBaseObject DoAll !');
 
  end;
 
  { Base class End }
 
 
 
type
 
 
 
  { THook Start}
 
  //Interface implementation on a derived class
 
  THook = class(TBaseObject, ITestInterface)
 
 
     procedure DoSomething;
 
     procedure DoSomething;
    //Contrary to what is stated on the FPC reference manual
+
     procedure DoItAll;
    //it's now possible to use aliases to indicate
 
    //interface implementations
 
     procedure ITestInterface.DoItAll = DoAll;
 
  end;
 
 
 
  procedure THook.DoSomething;
 
  begin
 
    Writeln('THook Doing something !');
 
  end;
 
  { THook End}
 
 
 
type
 
 
 
  { TRealClass Start }
 
  TRealClass = class(TBaseObject, ITestInterface)
 
  private
 
    FHook: ITestInterface;
 
  public
 
    constructor Create;
 
    destructor Destroy; override;
 
    //Here we tell the compiler that our class
 
    //interface implementation is provided by
 
    //a member field wich we initialize in this case
 
    //on the constructor
 
    property Hook: ITestInterface read FHook implements ITestInterface;
 
 
   end;
 
   end;
  
   constructor TRealClass.Create;
+
   procedure TBaseObject.DoSomething;
 
   begin
 
   begin
     //Creating our interface implementor
+
     Writeln('TBaseObject DoSomething !');
    FHook := THook.Create;
 
 
   end;
 
   end;
  
   destructor TRealClass.Destroy;
+
   procedure TBaseObject.DoItAll;
 
   begin
 
   begin
     //Releasing our interface implementor
+
     Writeln('TBaseObject DoItAll !');
    THook(FHook).Free;
 
    inherited Destroy;
 
 
   end;
 
   end;
  { TRealClass End }
 
 
 
var
 
var
  R: TRealClass;
 
 
   I: ITestInterface;
 
   I: ITestInterface;
 
 
begin
 
begin
 
   Writeln('Using regular interfaces');
 
   Writeln('Using regular interfaces');
 
+
   I := TBaseObject.Create as ITestInterface;
   R := TRealClass.Create;
+
   if I <> nil then
 
 
  //callign the interfaced function directly
 
  R.DoAll;
 
 
 
  //Calling through the interface
 
   if R.GetInterface(STestInterface, I) then
 
  begin
 
 
     Writeln('Got interface OK. Calling it');
 
     Writeln('Got interface OK. Calling it');
    I.DoSomething;
+
  I.DoSomething;
    I.DoItAll;
+
  I.DoItAll;
  end else
 
    Writeln('Interface not implemented !');
 
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== See also ==
 
== See also ==
  

Revision as of 17:09, 5 October 2021

Copy the text below and it will demonstrate how to use Interfaces to write less code, avoid code repetition,..., this is a fully working program.

{$mode objfpc}{$H+}{$interfaces com}
type
  ITestInterface = interface
    ['{3FB19775-F5FA-464C-B10C-D8137D742088}']
    procedure DoSomething;
    procedure DoItAll;
  end;

  TBaseObject = class(TInterfacedObject,ITestInterface)
    procedure DoSomething;
    procedure DoItAll;
  end;

  procedure TBaseObject.DoSomething;
  begin
    Writeln('TBaseObject DoSomething !');
  end;

  procedure TBaseObject.DoItAll;
  begin
    Writeln('TBaseObject DoItAll !');
  end;
var
  I: ITestInterface;
begin
  Writeln('Using regular interfaces');
  I := TBaseObject.Create as ITestInterface;
  if I <> nil then
    Writeln('Got interface OK. Calling it');
  I.DoSomething;
  I.DoItAll;
end.

See also