Ramifications

[Previous] [Next]

One advantage to using the Bridge design pattern is that ActiveX-enabled programming languages that do not support interface inheritance can now benefit from interface reuse.

Implementation inheritance, which is not supported by Visual Basic, provides reuse of implementations through a class hierarchy. A descendant class could potentially reuse implementations from all ancestor classes in its inheritance chain. Using the Bridge design pattern, you can accomplish the implementation reuse by having multiple implementor classes subscribe to the same event published by a single bridge. Each implementor class's event handler will be called as a result of a single client request.

The Bridge design pattern accomplishes the decoupling of an interface from its implementation, which results in the following benefits:

  • In conventional interface inheritance, any changes made to the properties or methods of an interface directly affect all supporting classes, which requires you to recompile and redeploy those classes. In the Bridge design pattern, modifications made to an interface do not directly affect the implementors—only the bridge class needs to be updated. Keep in mind that changing property and method signatures of an interface in Visual Basic constitutes a binary incompatibility with the previous version of the interface. Behind the "curtains," COM considers this update a new interface, not a change to an existing one. Visual Basic internally asks COM to generate a new interface identifier (IID) that uniquely identifies this interface throughout the universe. As a result, early bound clients have to reference the new interface and recompile accordingly.
  • Implementors can reside in a different ActiveX component from where the abstraction and the bridge and from where other implementors reside. Therefore:
    • Modification of an implementation requires a recompile of only the component in which the implementor resides.
    • New implementors can bind dynamically to a single interface by subscribing to events published by a given bridge.
    • Implementation can be hidden from the clients of an interface.

These benefits are also realized through interface inheritance and through the COM and ActiveX technologies that are an inherent part of Visual Basic. By using polymorphism, an interface can bind dynamically at run time to an object that supports it. Interfaces are made available through type libraries, and different components can all reference the same type library. This ability allows the components to define and change their implementations at will and requires a recompile of only the changed component. The only thing missing is the granularity at which an interface can bind to an object.

The Bridge design pattern adds this granularity. Implementations for all properties and methods of an interface are no longer bound to one class module. In other words, the Bridge design pattern allows a client to have a reference to a single instance of a bridge object that supports the interface the client expects. Yet the implementation for each property and method of the expected interface can be dispersed among different class objects. As a result,

  • Implementors can implement parts of an interface. Reusability shifts from the interface level to the functions and properties of the interface. Interfaces are viewed as contracts between the client and the object serving up the interface. In general, providing partial implementations of an interface is not good programming practice because it breaches the contract. A client expects references to all properties and methods of an interface to produce valid results.
  • Confusion might arise as to where the state of an object should reside—should the state be dispersed among multiple implementors, or should it reside in the bridge?

To avoid these potentially unacceptable results, you should make certain design decisions before you implement the Bridge design pattern. For instance, if you intend to have multiple implementors to implement parts of an interface, the bridge should provide default behavior if no subscribing implementors exist. Also, you should have the state reside solely in the bridge.



Microsoft Visual Basic Design Patterns
Microsoft Visual Basic Design Patterns (Microsoft Professional Series)
ISBN: B00006L567
EAN: N/A
Year: 2000
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net