Applied Design Patterns with Java
Structural :: Bridge (151) {C ch 10}
Implementation
Here are implementation issues to consider when using the
Bridge pattern:
- Only one Implementor. If there's only one implementation, creating an abstract Implementor class isn't necessary.
This is a degenerate case of the Bridge pattern; there's a one-to-one relationship between Abstraction
and Implementor. Nevertheless, this separation is still useful when a change in the implementation of a class must
not affect its existing clients. This allows hiding an implementation of a class completely from its clients.
- Creating the right Implementor object. Which Implementor class should be instantiated when there's more than one?
If Abstraction knows about all ConcreteImplementor classes, then it can instantiate one of them in its constructor;
it can decide between them based on parameters passed to its constructor. Another approach is to choose a default
implementation initially and change it later according to usage. It's also possible to delegate the decision to
another object altogether. A benefit of this approach is that Abstraction is not coupled directly to any of the
Implementor classes.
- Sharing implementors. It is possible to share implementations among several objects. A Body stores a reference
count that the Handle class increments and decrements.
- Using multiple inheritance. You can use multiple inheritance in C++ to combine an interface with its implementation.
But because this approach relies on static inheritance, it binds an implementation permanently to its interface.
Therefore you can't implement a true Bridge with multiple inheritance in C++. However, Java does allow multiple
interface inheritance.
Related Patterns
An Abstract
Factory (87) can create and configure a particular
Bridge.
The Adapter (139) pattern is geared toward making unrelated classes work together. It is usually applied
to systems after they're designed. Bridge is used up-front in a design to let abstractions and implementations
vary independently.
Catalog Structural Prev Next