July 21, 2003

Delegates vs. Interfaces

Don Box highlights some of the similarities and differences between using delegates or interfaces to integrate two pieces of code. This is always a decision I have struggled with in the past, and Don's example provides some interesting insight:

Both approaches require an explicit step to integrate the two pieces of code. The fundamental difference between the two approaches is that the delegate-based design requires that explicit step to take place at the point of integration, not at a particular type definition. The delegate-based approach specifically does not assume that the target method will have any awareness of its integration partner, rather, it's just a method that can be called by anyone with sufficient security/access permissions. In contrast, the interface-based approach requires the target method to have an a priori understanding of the integration contract (IPredicate in this example).

[ Don Box's Spoutlet - Are delegates the moniker of .NET? ]

Don's point is ultimately that delegates provide easier reuse of existing code, and thus help to create more loose coupling between components and libraries.

Entry categories: Dot NET
Posted by Jorgen Thelin at July 21, 2003 03:58 PM - [PermaLink]