After you read the material on the Internet, you probably should study the code of other programmers to find out how (and why) they used the interfaces in real code.
For example, in the Subversion repository of dUnit SourceForge, you will find XPObserver.pas , which implements the Observer template for Delphi using interfaces. This code is very interesting, since usually in this sample the observed objects store a list of observers, and each observer keeps a link to the objects (objects) that they observe. A naive implementation using interfaces would create circular references, interfaces would allow their reference counts to reach 0, and this would lead to memory leaks. The code in XPObserver.pas shows how you can use typecasting to solve this problem.
IMO, most XP * .pas files deserve close attention. For example, XPInterfacedObject.pas provides an explanation of why aggregated interfaces should use a common reference counter and present an alternative solution for TAggregatedObject and TContainedObject as implemented in VCL.
mghie
source share