Getting a grasp of interface-based programming is really tough. To gain an appreciation for this style of programming, you must leave behind old habits and intuitions about writing code and look back at the evolution of object-oriented programming (OOP) and computer science over the past decade—a Darwinian saga of how the interface has made modern software designs more fit for survival. For software to survive in the ever changing jungle of the production environment, it must have three distinct characteristics: reusability, maintainability, and extensibility. This chapter provides a general overview of interface-based programming and examines these characteristics.
COM is founded on the idea of interface-based programming, a style of programming in which the interface is separate from implementation. (COM has no meaning without the concept of the interface. Interfaces, on the other hand, can exist outside the world of COM.) Interface-based programming was pioneered in languages such as C++ and Smalltalk by software engineers who discovered that using distinct interfaces could make their software, especially large applications, easier to maintain and extend. (The creators of Java saw the elegance of interface-based programming and consequently built support for it directly into their language.)
Interfaces solve many problems associated with code reuse in object-oriented programming. This chapter investigates some of these problems. In particular, when you program in a style consistent with classic OOP, a client can build inflexible dependencies on a class definition. These dependencies can make it difficult to maintain or extend the class without breaking the client. It becomes tedious or impossible to improve the code for an object over time. Certain problems are also associated with a popular OOP language feature known as implementation inheritance. This powerful but often misused feature is vulnerable to similar dependency problems, which compromise an application's maintainability and extensibility. Even though Visual Basic doesn't support implementation inheritance, this chapter describes its strengths and limitations in order to address some of the problems that interface-based programming was created to solve.
Visual Basic 5.0 added support for defining and implementing user-defined interfaces. This chapter shows you how to use interfaces in a Visual Basic application. After covering the basics of using interfaces, it demonstrates how to achieve polymorphism and run-time type inspection, which make interface-based programming powerful.