Ikke
2011-01-25 21:31:27 UTC
Hi everybody,
There's something I don't really understand about interfaces. Suppose you
have the following situation:
an interface 'ISavable' (defined in a unit)
-----
type
ISavable = Interface(IInterface)
// Properties and their functions
function Load : boolean;
function Save : boolean;
end;
function Save : boolean;
begin
result := true;
end;
-----
a class implementing said interface (defined in a second unit)
-----
TWorld = Class(TInterfacedObject, ISavable)
private
{ Private declarations }
function Save : boolean;
public
{ Public declarations }
end;
function TWorld.Save : boolean;
begin
result := false;
end;
-----
and some code, creating a world in need of saving (in a third unit)
-----
procedure TForm1.SaveWorldClick(Sender: TObject);
var
world : ISavable;
begin
world := TWorld.Create;
if (world.Save) then
begin
ShowMessage('The world is saved!');
end
else
begin
ShowMessage('The world is lost!');
end;
FreeAndNil(world);
end;
-----
My first question: is this the proper way (in the interface) to provide a
default implementation? I suspect not, because if it were a class, I'd
need to prepend the function name with the class name.
Second question: the result of this little procedure is a showmessage
which tells me 'the world is lost'. How on earth can the Save function be
called from within the form, since it's private in the unit containing
the World class? Is it because the Save function is defined in the
interface, in other words, can I assume that private/public no longer
matters here?
Third, and last question: the FreeAndNil statement to destroy the world
object gives me a nice AccessViolation, but I can't see why. If world is
properly created, surely I should be able to destroy it?
Coming from a Java background, I fear I'm missing several parts of the
Delphi interface puzzle here - would anybody please be so kind as to
explain them to me? Thanks ever so much!
Best regards,
Ikke
There's something I don't really understand about interfaces. Suppose you
have the following situation:
an interface 'ISavable' (defined in a unit)
-----
type
ISavable = Interface(IInterface)
// Properties and their functions
function Load : boolean;
function Save : boolean;
end;
function Save : boolean;
begin
result := true;
end;
-----
a class implementing said interface (defined in a second unit)
-----
TWorld = Class(TInterfacedObject, ISavable)
private
{ Private declarations }
function Save : boolean;
public
{ Public declarations }
end;
function TWorld.Save : boolean;
begin
result := false;
end;
-----
and some code, creating a world in need of saving (in a third unit)
-----
procedure TForm1.SaveWorldClick(Sender: TObject);
var
world : ISavable;
begin
world := TWorld.Create;
if (world.Save) then
begin
ShowMessage('The world is saved!');
end
else
begin
ShowMessage('The world is lost!');
end;
FreeAndNil(world);
end;
-----
My first question: is this the proper way (in the interface) to provide a
default implementation? I suspect not, because if it were a class, I'd
need to prepend the function name with the class name.
Second question: the result of this little procedure is a showmessage
which tells me 'the world is lost'. How on earth can the Save function be
called from within the form, since it's private in the unit containing
the World class? Is it because the Save function is defined in the
interface, in other words, can I assume that private/public no longer
matters here?
Third, and last question: the FreeAndNil statement to destroy the world
object gives me a nice AccessViolation, but I can't see why. If world is
properly created, surely I should be able to destroy it?
Coming from a Java background, I fear I'm missing several parts of the
Delphi interface puzzle here - would anybody please be so kind as to
explain them to me? Thanks ever so much!
Best regards,
Ikke