[fpc-pascal] Specifying a Interface type via $Interfaces
dean.zobec at gmail.com
Thu Oct 11 18:48:25 CEST 2007
2007/10/11, Graeme Geldenhuys <graemeg.lists at gmail.com>:
> I want to start learning the use of Interfaces. It's a language
> feature I have by-passing for some time now. From what I have read
> this seems to be a handy language feature to learn.
> In the FPC documentation in mentions I can have COM or CORBA (Java)
> interface types. Could somebody please explain the difference. And
> why would I use one over the other?
Corba interfaces are not reference counted as they don't descend from IUnknown
For a good tutorial about the reasons to use interfaces from an OOP
point of view see this chapter of Delphi in a Nutshell by By Ray
In particular see this explanation:
"The most important use of interfaces is to separate type inheritance
from class inheritance. Class inheritance is an effective tool for
code reuse. A derived class easily inherits the fields, methods, and
properties of a base class, and thereby avoids reimplementing common
methods. In a strongly typed language, such as Delphi, the compiler
treats a class as a type, and therefore class inheritance becomes
synonymous with type inheritance. In the best of all possible worlds,
though, types and classes are entirely separate.
Textbooks on object-oriented programming often describe an inheritance
relationship as an "is-a" relationship, for example, a TSavingsAccount
"is-a" TAccount. You can see the same idea in Delphi's is operator,
where you test whether an Account variable is TSavingsAccount.
Outside of textbook examples, though, simple is-a relationships break
down. A square is a rectangle, but that doesn't mean you want to
derive TSquare from TRectangle. A rectangle is a polygon, but you
probably don't want to derive TRectangle from TPolygon. Class
inheritance forces a derived class to store all the fields that are
declared in the base class, but in this case, the derived class
doesn't need that information. A TSquare object can get away with
storing a single length for all of its sides. A TRectangle object,
however, must store two lengths. A TPolygon object needs to store many
sides and vertices.
The solution is to separate the type inheritance (a square is a
rectangle is a polygon) from class inheritance (class C inherits the
fields and methods of class B, which inherits the fields and methods
of class A). Use interfaces for type inheritance, so you can leave
class inheritance to do what it does best: inheriting fields and
In other words, ISquare inherits from IRectangle, which inherits from
IPolygon. The interfaces follow the "is-a" relationship. Entirely
separate from the interfaces, the class TSquare implements ISquare,
IRectangle, and IPolygon. TRectangle implements IRectangle and
> COM sounds very Windows specific, so should I rather use CORBA
> interface types. All my software target multiple platforms. I mostly
> develop under Linux though.
No it's mainly a matter of reference counting
> Anybody got some good links on information about Interfaces? I'm
> looking for some nice tutorials if possible.
I'll try to find some old links about articles I've read years ago.
More information about the fpc-pascal