From the article: Interfaces in Delphi Programming 101
Most Delphi developers when they think of interfaces they think of COM programming. However, interfaces are just an OOP feature of the language - they are not tied to COM specifically.
Interfaces can be defined and implemented in a Delphi application without touching COM at all.
Have you ever created (and implemented) your own interface? Why?
Share with us some real-world examples in using interfaces in Delphi applications... Why do you do Interfaces?
Adding functionality to existing classes
- Interfaces are a nice way of asking if an object "supports some functionality". We could do this by using "is" (eg: "if obj is TWinControl") but this requires objects to fall within a certain class hierarchy implementing the required functionality. I use interfaces for a design application where objects need functionality like printing. If I rely on class hierarchy to determine if objects support common functionality then I have to either a) re-create an entire control class hierarchy (buttons, edits, labels, memos etc) that include a base class with the common functionality or b) implement the common functionality in a non-OOP if-then-else construct. With interfaces, I can derive new classes directly from existing classes (I don't have to re-write my own versions of TLabel, TEdit, TMemo etc) and simply implement the new interfaces. To see if a control has the functionality we simply ask if the object "supports" the interface.
- —Guest Etherman
Why do I use interfaces
- I use interfaces to share methods and properties in classes that do not inherit from the same parents. For instance to create a set of data-aware controls having common methods like Load, Save, AsText, ...
- —Guest Pascal Piret
Interfaces sounded too complex for me...
- But after reading this article ... I see dozens of places in my applications where interfaces will come handy. Thanks!
- —rascal1936

