Difference between revisions of "How To Use Interfaces"
From Lazarus wiki
Jump to navigationJump to searchm (Added another category) |
|||
(9 intermediate revisions by 2 users not shown) | |||
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. | ||
+ | The default interface in FPC is COM, which makes that once a class with an interface is instantiated as that interface it will automatically be released. | ||
+ | Note if you change the interface type to CORBA it WILL leak. | ||
− | <syntaxhighlight lang="pascal"> | + | This example has two unrelated classes, but share the same COM interface. |
− | program interfacesygenerics; | + | Since they are instantiated through that interface, destruction is automatic: |
− | + | <syntaxhighlight lang="pascal">program interfacesygenerics; | |
{$mode objfpc}{$H+} | {$mode objfpc}{$H+} | ||
− | |||
− | |||
− | |||
− | |||
type | type | ||
ITestInterface = interface | ITestInterface = interface | ||
− | [ | + | ['{3FB19775-F5FA-464C-B10C-D8137D742088}'] |
procedure DoSomething; | procedure DoSomething; | ||
procedure DoItAll; | procedure DoItAll; | ||
end; | end; | ||
− | + | TOneObject = class(TInterfacedObject,ITestInterface) | |
− | + | procedure DoSomething; | |
− | procedure | + | procedure DoItAll; |
− | |||
− | |||
− | |||
− | |||
− | |||
end; | end; | ||
− | |||
− | |||
− | |||
− | + | TOtherObject = class(TInterfacedObject,ITestInterface) | |
− | |||
− | |||
procedure DoSomething; | procedure DoSomething; | ||
− | + | procedure DoItAll; | |
− | |||
− | |||
− | procedure | ||
end; | end; | ||
− | + | ||
− | procedure | + | procedure TOneObject.DoSomething; |
begin | begin | ||
− | Writeln(' | + | Writeln('TOneObject DoSomething !'); |
end; | end; | ||
− | |||
− | + | procedure TOneObject.DoItAll; | |
− | + | begin | |
− | + | Writeln('TOneObject DoItAll !'); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end; | end; | ||
− | + | ||
− | + | procedure TOtherObject.DoSomething; | |
begin | begin | ||
− | + | Writeln('TOtherObect DoSomething !'); | |
− | |||
end; | end; | ||
− | + | procedure TOtherObject.DoItAll; | |
begin | begin | ||
− | + | Writeln('TOtherObect DoItAll !'); | |
− | |||
− | |||
end; | end; | ||
− | + | ||
− | |||
var | var | ||
− | + | I: ITestInterface = nil; | |
− | I: ITestInterface; | ||
− | |||
begin | begin | ||
Writeln('Using regular interfaces'); | Writeln('Using regular interfaces'); | ||
− | + | I := TOneObject.Create as ITestInterface; | |
− | + | if I <> nil then | |
− | + | Writeln('Got interface OK. Calling it'); | |
− | + | I.DoSomething; | |
− | + | I.DoItAll; | |
− | + | I:=nil; // releases TOneObject | |
− | // | + | I := TOtherObject.Create as ITestInterface; |
− | if | + | if I <> nil then |
− | |||
Writeln('Got interface OK. Calling it'); | Writeln('Got interface OK. Calling it'); | ||
− | + | I.DoSomething; | |
− | + | I.DoItAll; | |
− | + | end.</syntaxhighlight> | |
− | |||
− | end. | ||
− | </syntaxhighlight> | ||
− | |||
== See also == | == See also == | ||
Latest revision as of 09:24, 6 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. The default interface in FPC is COM, which makes that once a class with an interface is instantiated as that interface it will automatically be released. Note if you change the interface type to CORBA it WILL leak.
This example has two unrelated classes, but share the same COM interface. Since they are instantiated through that interface, destruction is automatic:
program interfacesygenerics;
{$mode objfpc}{$H+}
type
ITestInterface = interface
['{3FB19775-F5FA-464C-B10C-D8137D742088}']
procedure DoSomething;
procedure DoItAll;
end;
TOneObject = class(TInterfacedObject,ITestInterface)
procedure DoSomething;
procedure DoItAll;
end;
TOtherObject = class(TInterfacedObject,ITestInterface)
procedure DoSomething;
procedure DoItAll;
end;
procedure TOneObject.DoSomething;
begin
Writeln('TOneObject DoSomething !');
end;
procedure TOneObject.DoItAll;
begin
Writeln('TOneObject DoItAll !');
end;
procedure TOtherObject.DoSomething;
begin
Writeln('TOtherObect DoSomething !');
end;
procedure TOtherObject.DoItAll;
begin
Writeln('TOtherObect DoItAll !');
end;
var
I: ITestInterface = nil;
begin
Writeln('Using regular interfaces');
I := TOneObject.Create as ITestInterface;
if I <> nil then
Writeln('Got interface OK. Calling it');
I.DoSomething;
I.DoItAll;
I:=nil; // releases TOneObject
I := TOtherObject.Create as ITestInterface;
if I <> nil then
Writeln('Got interface OK. Calling it');
I.DoSomething;
I.DoItAll;
end.