Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the pros and cons of using interfaces in Delphi?

I have used Delphi classes for a while now but never really got into using interfaces. I already have read a bit about them but want to learn more.

I would like to hear which pros and cons you have encountered when using interfaces in Delphi regarding coding, performance, maintainability, code clearness, layer separation and generally speaking any regard you can think of.

Thanks and best regards

like image 802
Guillem Vicens Avatar asked Feb 01 '11 10:02

Guillem Vicens


People also ask

Why use interface in Delphi?

In Delphi, "interface" has two distinct meanings. In OOP jargon, you can think of an interface as a class with no implementation. In Delphi unit definition interface section is used to declare any public sections of code that appear in a unit. This article will explain interfaces from an OOP perspective.


2 Answers

All I can think of for now:

Pros:

  • Clear separation between interface and implementation
  • Reduced unit dependencies
  • Multiple inheritance
  • Reference counting (if desired, can be disabled)

Cons:

  • Class and interface references cannot be mixed (at least with reference counting)
  • Getter and setter functions required for all properties
  • Reference counting does not work with circular references
  • Debugging difficulties (thanks to gabr and Warren for pointing that out)
like image 156
jpfollenius Avatar answered Sep 26 '22 17:09

jpfollenius


Adding to the answers few more advantages:

  1. Use interfaces to represent the behavior and each implementation of a behavior will implement the interface.
  2. API Publishing: Interfaces are great to use when publishing APIs. You can publishing an interface without giving out the actual implementation. So you are free to make internal structural changes without causing any problems to the clients.
like image 24
Bharat Avatar answered Sep 23 '22 17:09

Bharat