Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Interfaces

Moderators: larsivi kris

Posted: 02/04/08 03:32:08

Tango Team,

First, I have to send my congrats for the new book; it's a great source of information and an interesting read.

I've been reading Chapter 3, and I'm a little confused about the usefulness of Interfaces. Since an Interface only describes the methods that the implementing class should have, I don't quite understand the practical application. "Learn to Tango with D" and the Digital Mars online documentation do a descent job in giving a general description, although it would be nice to see a practical example.

Any help or direction would be appreciated.

Thanks, Mason

Author Message

Posted: 02/04/08 03:51:59 -- Modified: 02/04/08 03:56:16 by
kris

Interfaces represent contracts between two or more parties. If you can agree on the contract, then the implementation matters much less. Thus, interfaces do not have implementation. A clean interface should only import other interfaces, and not implementation of any kind.

They're basically a decoupling mechanism, used to assist in breaking up potentially large software projects. You can also use an interface to describe a plug-in design, where (obviously) the implementation would be quite different for each plug-in. The difference between an abstract base-class and an interface is that the latter has no implementation at all. Because of this attribute, a class can implement multiple interfaces (and thus masquerade as any of them). To do so using abstract classes would require multiple-inheritance, which D does not have.

From a practical aspect, Tango uses a handful of interfaces in order to decouple dependencies between certain areas of the library. This helps to make the library considerably more modular. Hope that helps somewhat :)

- kris

Posted: 02/05/08 16:32:25

Kris,

This is great information:

kris wrote:

Interfaces represent contracts between two or more parties. If you can agree on the contract, then the implementation matters much less. Thus, interfaces do not have implementation. A clean interface should only import other interfaces, and not implementation of any kind.

I would propose that this will make excellent content for the 2nd edition, or at least the Tango tutorials!

Thanks, Mason