Last active
January 23, 2022 11:02
-
-
Save omonien/1c95589bc7f3cca325615c7aad3334b0 to your computer and use it in GitHub Desktop.
Simple Interface demo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program Interfaces; | |
{$APPTYPE CONSOLE} | |
{$R *.res} | |
uses | |
LeakCheck, | |
System.Classes, System.SysUtils; | |
type | |
IFoo = interface(IInterface) | |
['{EB3BBAA4-6176-48E1-9419-2D6B2B5C6B67}'] | |
procedure Something; | |
end; | |
TFoo = class(TInterfacedObject, IFoo) | |
public | |
procedure Something; | |
end; | |
procedure DoSomething; | |
begin | |
//Create an instance of TFoo | |
var LFoo: IFoo := TFoo.Create; | |
//Do something with that instance | |
LFoo.Something; | |
//No need to cleanup. LFoo goes out of scope, which will trigger TFoo's destructor. | |
end; | |
{ TFoo } | |
procedure TFoo.Something; | |
begin | |
Writeln('Something'); | |
end; | |
begin | |
ReportMemoryLeaksOnShutdown := true; | |
try | |
//Do esomething that creates a couple of TFoo instances | |
DoSomething; | |
DoSomething; | |
// All instances of TFoo are gone here, as they are no longer in use here. | |
except | |
on E: Exception do | |
Writeln(E.ClassName, ': ', E.Message); | |
end; | |
Readln; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Two notes: