Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-10

Specifying a Interface type via $Interfaces

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

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?

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.

Anybody got some good links on information about Interfaces? I'm looking for some nice tutorials if possible.

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Cesar Romero
to Graeme Geldenhuys

Graeme,

Hi,

Hi

Show quoted text 8 lines
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?

Im not sure, but I think that the one difference is about time life: COM uses ref count
CORBA should be somethink like garbage collection

[]s

Cesar Romero

Reply by email

Dean Zobec
to Cesar Romero

2007/10/11, Cesar Romero :

Show quoted text 17 lines
Graeme, > Hi, > > Hi > 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? > Im not sure, but I think that the one difference is about time life: COM uses ref count CORBA should be somethink like garbage collection

There's no garbage collection, you have to free the object instance explicitly with the
Corba type of interfaces in FPC. Corba interfaces do not descend from IUnknown.

Dean

Reply by email

Dean Zobec
to Graeme Geldenhuys: I want to start learning the use of Interfaces. It's a language feature I have by-passing…

2007/10/11, Graeme Geldenhuys :

Show quoted text 9 lines
Hi, 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 Lischner:
http://www.oreilly.com/catalog/delphi/chapter/ch02.html

In particular see this explanation:

Quoting:
"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 methods.

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 IPolygon."

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.

Ciao,
Dean

Reply by email

Graeme Geldenhuys
to Dean Zobec
Dean Zobec wrote
> 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

So other than that, COM interfaces and CORBA interfaces share the same features? As for reference counting - that just means that as soon as the reference count for an Interfaces reaches zero, it gets automatically freed? CORBA then rather treats them like classes - you can have no other objects referencing the class, yet it stays in memory. You always need to free it off manually.

point of view see this chapter of Delphi in a Nutshell by By Ray Lischner: http://www.oreilly.com/catalog/delphi/chapter/ch02.html

Thanks for the link and quote. It already makes more sense. Hopefully more reading about the topic will strengthen the knowledge when to use Interfaces and when to use Class Inheritance.

I'll try to find some old links about articles I've read years ago.

That will be very much appreciated. Thanks.

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Dean Zobec
to Graeme Geldenhuys

2007/10/11, Graeme Geldenhuys :

Show quoted text 6 lines
On 11/10/2007, Dean Zobec wrote: > > 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 So other than that, COM interfaces and CORBA interfaces share the same features?

yes IIRC

As for reference counting - that just means that as soon as the reference count for an Interfaces reaches zero, it gets automatically freed?

yes, though you have to pay much attention to memory leaks and possible problems when dealing a lot with interfaces, memory leaks should be monitored closely.

CORBA then rather treats them like classes - you can have no

other objects referencing the class, yet it stays in memory. You always need to free it off manually.

Yes, like dealing with normal objects.

Show quoted text 11 lines
> point of view see this chapter of Delphi in a Nutshell by By Ray > Lischner: > http://www.oreilly.com/catalog/delphi/chapter/ch02.html Thanks for the link and quote. It already makes more sense. Hopefully more reading about the topic will strengthen the knowledge when to use Interfaces and when to use Class Inheritance. > I'll try to find some old links about articles I've read years ago. That will be very much appreciated. Thanks.

Time has deleted a lot of web links, funny how much data and knowledge gets lost when the webspace is gone.
I'm sure I'll be able to find something shuffling from the old delphi CDs or papers.
A good article about the use of interfaces in free pascal is really needed. I think interfaces are a very powerful oop feature. If only I had time :(

regards,
Dean

Show quoted text 12 lines
Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/ _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Dean Zobec
to Graeme Geldenhuys: So other than that, COM interfaces and CORBA interfaces share the same features? As for…

2007/10/11, Graeme Geldenhuys :

Show quoted text 9 lines
On 11/10/2007, Dean Zobec wrote: > > 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 So other than that, COM interfaces and CORBA interfaces share the same features? As for reference counting - that just means that as soon as the reference count for an Interfaces reaches zero, it gets automatically freed?

for a good example of refcounting interfaces you can take a look at the money.pp fpcunit example and the relative tests. fcl-fpcunit/src/exampletests/money.pp

Reply by email

Joao Morais
to Dean Zobec: 2007/10/11, Graeme Geldenhuys : yes IIRC yes, though you have to pay much attention to…
Dean Zobec wrote 6 lines
> As for reference counting - that just means that as soon as the > reference count for an Interfaces reaches zero, it gets automatically > freed? yes, though you have to pay much attention to memory leaks and possible problems when dealing a lot with interfaces, memory leaks should be monitored closely.

An special attention with circular references, ie, one interface whose instance holds a reference to another interface whose instance ... holds a reference to the first one. The result is an island of leaked objects. There are afaik three ways to solve this problem: garbage collection, an algorithm to detect the island or one weak reference in order to break the chain.

Joao Morais

Reply by email