The Bridge (Chapter 5), the Smart Proxy (Chapter 6), and the Event Service (Chapter 13) design patterns are all basically adapters, because they provide interfaces expected by
As defined in Chapter 2, interface inheritance is the means by which you obtain reusability in Microsoft Visual Basic. Different classes can inherit the same interface. Each class can then define a unique implementation for that interface. An object exposes the interfaces supported by the class the object is based on. Communication with an object is possible only through the interfaces it exposes.
For this reason, a Visual Basic object-oriented developer programs to an interface rather than to a class. As a result, an ideal system is said to exist when the
, which initiates interaction with one or more interfaces, is written only once. The objects that are bound to the interfaces used by the consumer can easily be
As you'll also recall from Chapter 2, reusing an interface requires you to use the keyword
, followed by the interface
The Bridge design pattern breaks the direct and permanent link between the interface and the implementation
The Bridge design pattern is useful in the following circumstances:
Your MIS manager has come to the realization that all application systems developed and supported by your department should retain personal configurations for each
In an effort to minimize the possibility that this situation might occur, your manager pitches an idea of maintaining user profiles for system elements such as user interface (UI) settings (such as window
You will be building a User Profile Service framework—packaged in an ActiveX DLL—that creates and stores user profile objects and retrieves those objects upon request from a given user profile repository. (See Chapter 11, "Repository," for more on this type of design pattern.) The User Profile Service framework takes into account the list of possible attributes that could be stored in a user profile by defining one UserProfile class that is flexible enough to contain any number of attributes of any given type. In addition, the User Profile Service framework defines a UserProfileRepository interface, which provides a uniform interface for concrete
Exposing the UserProfileRepository interface to consumers of your User Profile Service framework might seem sufficient for making your framework reusable and extensible. But you must not forget that your framework exists in an ActiveX component; therefore, the benefits of your framework are not limited to just Visual Basic consumers. Any programming language that supports ActiveX can profit in the same manner—however, not all programming languages are created equal.
For instance, let's assume you have applications in your department that are add-ins to Excel 97. (If you've upgraded to Excel 2000, you don't need to worry about this because Excel 2000 supports interface inheritance.) It's important to your manager that these applications retain user profiles. You're in luck because Excel 97 supports the use of ActiveX technologies, such as the ActiveX DLL where the User Profile Service framework resides. With the assistance of Excel's robust macro language technology (which was completely
Incorporating a Bridge design pattern in your User Profile Service framework will allow Excel 97 VBA programmers to extend your framework to support an Excel 97 workbook repository without any disruption to the framework code. As
Figure 5-1. Class diagram of the User Profile Service framework.
For each method defined in the UserProfileRepository interface, the Bridge class defines an analogous event that can be captured by event handlers defined in other classes. In this scenario, the consumer of these events is an Excel 97 workbook repository class defined in an Excel 97 add-in. (See Figure 5-2.) This particular implementation of the Bridge design pattern delegates method invocations from the UserProfileRepository interface to any class object that subscribes to the event.
Figure 5-2. Class diagram showing the relationship between the UserProfileRepository interface and the implementation of that interface defined in the Excel 97 workbook repository class.