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...
- I use interfaces to requery datasets that are already open, for example a list of cities. If you add or edit a city name, you can make the data refresh in any form you want where the city name may affect the opened form's data. Very useful.
For frames sharing printability
- My application have a toolbox of frames of which most of them are able to add them self to a printer-canvas. These implement the interface.
- —Guest henrik
Use interface to restrict MDI app
- define a mdi child form implement a interface, to execute some common features. and use a interface to restrict two main form: one for test, another for the actual.
- I use interfaces as an easy way to access objects and data in an external DLL. Great way for creating plugins and system extentions without having to re issue a full system release. Albiet the implementations can be quite big (code wise) but still alot more structured way to modularise my code and systems.
- —Guest Brendan McLaughlin
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!