Bridge The decoupling of abstraction and implementation
Ideas GoF Intentions –Decouple an abstraction from it implementation so that the two can vary independently Metsker –Focuses on the implementation of an abstraction
Other Structural Patterns Adapter Composite Decorator Façade Flyweight Proxy
Uses You want to avoid a permanent binding between an abstraction and it's implementation. This might be the case, for example, when the implementation must be selected or switched at run-time. Both the abstractions and their implementations should be extensible by sub-classing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently. Changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have been recompiled.
Real Uses What are some common uses of the Bridge pattern? –Drivers Applications System drivers –Window Interfaces X Window System Microsoft Windows –Application Programming Interfaces MFC JDBC
UML Diagram
Drivers JDBC is an application programming interface for executing SQL statements The drivers for JDBC and the applications that use JDBC are completely independent of each other –The same application can use an Oracle driver just as easily as a MySQL or Postgres driver
Challenge 6.1 Fill in the missing type names and the missing message name following illustration Class.forName(driverName); Connection c = DriverManager.getConnection(url, user, passwd); Statement s = c.createStatement(); ResultSet r = s.executeQuery(“select name, apogee from fireworks”); while(r.next()) { String name = r.getString(“name”); int apogee = r.getInt(“apogee”); System.out.println(name + “, “ + apogee); }
Challenge 6.1 Continued Fill in the missing type names and the missing message name following illustration
Abstract Class Refactoring Sometimes it is advantageous to refactor an abstract class to a Bridge pattern to gain more flexibility in the abstraction For instance, to separate the real-world implementation and a tester implementation
MachineController Refactoring Subclasses of MachineController exist for various types of machines and for various types of controllers Steps to refactor: –Move the abstract operations in the superclass into an interface –Define implementation classes that provide different implementations of the interface –Redefine the remaining operations in the abstract class as operations of the new interface
Challenge 6.3 Fill in the missing labels for MachineController interface
Summary Decoupling interface and implementation Improved extensibility Hiding implementation details from clients Other Patterns and Bridge –Abstract Factor can create and configure a particular Bridge –The Adapter and Bridge patterns are similar, but usually the Adapter pattern is used after the classes are created where as the Bridge pattern is used from in the ground-up design