Recent

Author Topic: Advantages of (I)Interface  (Read 11654 times)

icey

  • New Member
  • *
  • Posts: 19
Advantages of (I)Interface
« on: September 17, 2012, 09:39:34 pm »
Hi,

maybe - or better probably  :-[ - i'm just to stupid to get it, but what are the advantages of interfaces? As far as i understand it from the few tutorials i found, if i extend classes with an interface i still have to declare all the methods and properties within the class - so why shouldn't i just add those to the class itself?

dtoffe

  • Jr. Member
  • **
  • Posts: 65

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4698
  • I like bugs.
Re: Advantages of (I)Interface
« Reply #2 on: September 17, 2012, 10:38:59 pm »
maybe - or better probably  :-[ - i'm just to stupid to get it, but what are the advantages of interfaces?

Please look at Java's container and iterator interfaces and you get an idea of the advantages.
The classes implementing a certain interface don't need to inherit from the same class, and the code using the interface can easily switch to another implementation.
You can find much information about this topic and I will not repeat it here ...

However, my personal experience is that interfaces are overrated. Yes!
You can do the same things, and more, with abstract base classes.
In reality the code using the interfaced code often needs to access data behind the interface. However only methods can be defined for interface, not data.
For this reason the interfaces often have Get... and Set... methods just for the data access, and the implementing classes must define the data variables separately.
It is ok, but an abstract base class could do the same thing more easily. You can define variables in abstract classes.

Only if you implement many interfaces by one class, then the abstract base class approach does not work.
Interface can then solve the same problem that multiply inheritance does but without its negative side-effects.

So, using interfaces can lead to elegant design like Java's (container etc.) classes prove, but it requires more careful thinking across the whole library.
Partly it is a design decision. You can make an elegant design also by using abstract base classes.
For example the biggest Object Pascal app I know, Lazarus, uses lots of those base classes. I don't see how it would improve by using interfaces.

Object Pascal does not have a culture of using interfaces much.
It is mainly because the language has a serious design flaw: interfaces and memory management are mixed together!
This was a decision made by Borland a long time ago and was needed for Windows COM programming.
Support for reference counted objects is nice but binding it automatically to interfaces is a big mess and source of confusion. FPC has implemented another interface type which is not connected to memory management. Most people don't know it, I don't remember the exact syntax either now.


Regards,
Juha

P.S.
I don't like some other aspects of Java's libraries but IMO Java's container classes and their interfaces shine.
« Last Edit: September 17, 2012, 10:59:49 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

icey

  • New Member
  • *
  • Posts: 19
Re: Advantages of (I)Interface
« Reply #3 on: September 18, 2012, 09:30:24 am »
Thanks for your detailed answer. After reading some java stuff - like you suggested - i think i got the basic principles. At least i got enough to stick with abstract classes for now. Those i know how to use  :D

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Advantages of (I)Interface
« Reply #4 on: September 18, 2012, 12:05:05 pm »
Support for reference counted objects is nice but binding it automatically to interfaces is a big mess and source of confusion. FPC has implemented another interface type which is not connected to memory management. Most people don't know it, I don't remember the exact syntax either now.
The compiler directive needed is {$interfaces CORBA}
Thereafter, any interface defined and used is a CORBA interface, not a COM interface, so it does not descend from IUnknown, and is identified (if at all) by a simple string, not a GUID. CORBA interfaces, unlike COM interfaces can have properties which operate via getter and setter methods.
The compiler does no reference counting for CORBA interfaces. If you want that, you have to implement it yourself (or use a COM interface, which is the default).
However, the absence of reference counting is the main advantage of CORBA interfaces over COM - you get the advantage of interface syntax for designing an elegant class hierarchy without the "mess" (as Juha indicated) of automatic memory management, which often is not wanted in a clean design.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Advantages of (I)Interface
« Reply #5 on: September 18, 2012, 06:20:36 pm »
Java uses CORBA, which is why it doesn't suffer from automatic memory management of COM (it's more like an evil than angel for me), and their collections framework makes a good use of interface.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4698
  • I like bugs.
Re: Advantages of (I)Interface
« Reply #6 on: September 18, 2012, 08:51:33 pm »
The compiler directive needed is {$interfaces CORBA}

Yes, thanks.
Unfortunately this syntax is not used much, at least I have not seen it.
It may be because Delphi does not support it and most people don't know about it.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12721
  • FPC developer.
Re: Advantages of (I)Interface
« Reply #7 on: September 18, 2012, 10:16:38 pm »


Please look at Java's container and iterator interfaces and you get an idea of the advantages.
The classes implementing a certain interface don't need to inherit from the same class, and the code using the interface can easily switch to another implementation.
You can find much information about this topic and I will not repeat it here ...

However, my personal experience is that interfaces are overrated. Yes!
You can do the same things, and more, with abstract base classes.

Hmm. Interfaces allow for classes to inherit from multiple ancestors. Multiple inheritance can't be done using abstract baseclasses in Pascal. (it can be in C++).

Quote
In reality the code using the interfaced code often needs to access data behind the interface. However only methods can be defined for interface, not data.
For this reason the interfaces often have Get... and Set... methods just for the data access, and the implementing classes must define the data variables separately.

Note that classes can actually delegate interfaces to properties (implemented by other classes ) using the implements statement (though this only partially works in FPC).

Quote

So, using interfaces can lead to elegant design like Java's (container etc.) classes prove, but it requires more careful thinking across the whole library.

Can java do the delegation btw?

Quote
Partly it is a design decision. You can make an elegant design also by using abstract base classes.
For example the biggest Object Pascal app I know, Lazarus, uses lots of those base classes. I don't see how it would improve by using interfaces.

It has pretty much a single class hierarchy. Problems only appear if you have multiple ones. (and the only place where Lazarus has something
like that, it has trouble, resorting to hacks where widgetset hierarchy and LCL hierarchy interact)

Quote
Object Pascal does not have a culture of using interfaces much.
It is mainly because the language has a serious design flaw: interfaces and memory management are mixed together!

No, that is not true. The reason is much simpler. RTL and VCL were pretty much designed and implemented and thus set in stone
before interfaces were introduced in D4.

 

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4698
  • I like bugs.
Re: Advantages of (I)Interface
« Reply #8 on: September 18, 2012, 11:06:10 pm »
Hmm. Interfaces allow for classes to inherit from multiple ancestors. Multiple inheritance can't be done using abstract baseclasses in Pascal. (it can be in C++).
...
Note that classes can actually delegate interfaces to properties (implemented by other classes ) using the implements statement (though this only partially works in FPC).
...
Can java do the delegation btw?

Yes, I forgot to mention delegate interfaces. And yes, Java can do it and I have understood it is improving in FPC, too.

Quote
It has pretty much a single class hierarchy. Problems only appear if you have multiple ones. (and the only place where Lazarus has something
like that, it has trouble, resorting to hacks where widgetset hierarchy and LCL hierarchy interact)

Lazarus has lots of *Intf units: MainIntf, ProjectIntf, SrcEditorIntf etc. That works well.
The widgetset bindings could benefit from CORBA style interfaces.
Martin was planning some different hack there. Maybe he sees this and comments :)


Quote
No, that is not true. The reason is much simpler. RTL and VCL were pretty much designed and implemented and thus set in stone
before interfaces were introduced in D4.

Ok, maybe.

Regards,
Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Advantages of (I)Interface
« Reply #9 on: September 19, 2012, 09:44:14 am »
I'd say one of the main advantages of interfaces are that there language independent.
As such are very handy for creating OOP based library's.

Take DirectX for example, most likely created in say MS Visual Studio, but accesseable from  other languages like Delphi etc, but in a OOP way.  If on the other hand DirectX had been implemented using static procedures, it would have been mess.

The other advantages already mentioned are useful too.  A Multi-Inheritance access method, auto-lifetime with IUnknown.  etc.

If say Lazarus/FPC decided to make a sort of .net framework DLL, interfaces would come in very handy.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4698
  • I like bugs.
Re: Advantages of (I)Interface
« Reply #10 on: September 19, 2012, 06:52:11 pm »
The other advantages already mentioned are useful too.  A Multi-Inheritance access method, auto-lifetime with IUnknown.  etc.

The auto-lifetime management is conceptually very different from interface. It is counter-intuitive that they are tied together.
That is why I called it a design flaw.

FPC has improved the situation with CORBA style interfaces. Now you can define an interface without pulling in the reference counting.
The opposite is still not possible. You cannot create a reference counted object without defining an interface for it.

About interfaces being language independent....
"Interface" as discussed here is not enough for connecting to external libraries.
The term is flexible though. You can talk about "interface to an external library" even if you don't use the "interface" keyword in the code.


Juha
« Last Edit: September 19, 2012, 07:30:47 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Advantages of (I)Interface
« Reply #11 on: September 20, 2012, 08:25:24 am »
Quote
The auto-lifetime management is conceptually very different from interface

Talking about IInterface, without pointing out IUnknown would seem very odd.  You don't get auto-lifetime on standard classes after all.   In a way, that is a shame, it would be nice if Pascal/FPC had a way of making a standard class into a managed type.  (or is they?.  so many hidden gems in FPC, you never know).

Quote
FPC has improved the situation with CORBA style interfaces.

I've never used CORBA, but from what I can tell it's IDL based, so wound't this be slower?.   IInterface is just a sort of VMT mapping.   But I assume a big advantage of CORBRA here is it's cross-platform nature, I assume it handles big/little endian's automatically etc.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4698
  • I like bugs.
Re: Advantages of (I)Interface
« Reply #12 on: September 20, 2012, 05:50:04 pm »
Talking about IInterface, without pointing out IUnknown would seem very odd.  You don't get auto-lifetime on standard classes after all.   In a way, that is a shame, it would be nice if Pascal/FPC had a way of making a standard class into a managed type.  (or is they?.  so many hidden gems in FPC, you never know).

I was talking about "interface", not  "IInterface".
I agree with you, it is not intuitive that interface and memory-management are tied together.

Quote
I've never used CORBA, but from what I can tell it's IDL based, so wound't this be slower?.   IInterface is just a sort of VMT mapping.   But I assume a big advantage of CORBRA here is it's cross-platform nature, I assume it handles big/little endian's automatically etc.

No, you are mixing different things now (again). The CORBA style interface does not actually use CORBA (Common Object Request Broker Architecture).
It is very confusing, yes. The FPC guys could have picked a better name. This one should be "normal interface", the other one could be called "Delphi interface".

Also, interfaces don't use VMT which is strictly about class inheritance and virtual methods.
Interfaces and class inheritance are conceptually related though.


Regards,
Juha
« Last Edit: September 20, 2012, 07:50:59 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Advantages of (I)Interface
« Reply #13 on: September 20, 2012, 08:55:31 pm »
Quote
No, you are mixing different things now (again).


Quiet possibly.   8)

There is only two types of interfaces I can think of,
1. The interface / implementation keywords.
2. class interfaces, as in    TMyObject = class(TObject, IInterface1, IInterface2).. etc. IInterface1 then been IInterface1 = interface(IInterface/IUnknown etc),  here is were I'd say IInterface1,IInterface2 is like a dummy VMT, in fact early versions of Delphi implemented these using virtual abstract classes.

So, from what I can gather your saying, there is a third..?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12721
  • FPC developer.
Re: Advantages of (I)Interface
« Reply #14 on: September 21, 2012, 03:08:16 pm »

No, you are mixing different things now (again). The CORBA style interface does not actually use CORBA (Common Object Request Broker Architecture).
It is very confusing, yes. The FPC guys could have picked a better name. This one should be "normal interface", the other one could be called "Delphi interface".

Worse, Interfaces in the Corba sense ARE reference counted, yet FPC's "corba" interfaces are not.

Delphi decided to deal with the COM interfaces in a automated way, and after a discussion with the usual anti Windows/Delphi hysterics and "we should do it better" attitude,
they didn't look much further and misnamed it.

So yes, Corba interfaces are generally refcounted (when used from Java).

And like most of such FPC own inventions, corba interfaces are relatively unused.
 
To be honest, I never understood what the problem with COM interfaces was. I use them daily. Yes, there are some rules about them, but so does manual usage.
« Last Edit: September 21, 2012, 03:10:03 pm by marcov »

 

TinyPortal © 2005-2018