8

I understand bridge pattern to some extent. I understand the decoupling of the interface and implementation. It uses an implementor class like a plugin which holds the actual logic of the derived class implementation.

But can some one explain how it allows the interface and derived to evolve independently? If I want to add a new method to the interface, it will have to be implemented in the derived class which will modify it.

Secondly, the client code has to be modified to set the new implementor when new object is needed.

1

3 Answers 3

22

Yes, the intent of the Bridge patter is to decouple abstraction (i.e., interface) from implementation, to let them vary independently. In practice, the idea is to use two separate hierarchies instead of the classic single hierarchy.

Let's make an example. Suppose that you have a Window abstraction, and you need to create an IconWindow subclass that specializes Window for every supported platform.

With a single hierarchy, you get:

                     Window
                       |--------------...
                   IconWindow
                       |
   -----------------------------------------------...
   |                   |                   |
XIconWindow       MSIconWindow      OSXIconWindow

This structure is very unconvenient, because:

  • if you want to add a new subclass that specializes Window (e.g., BitmapWindow) you must create a subclass for each supported platform (i.e., three subclasses in this example).

  • If you want to add a supported platform, you must add a new subclass for each existing specialization.

Therefore, it is better to decouple the two hierarchies by having:

             imp
      Window--------------------------> WindowImp
         |                                  |
    -----------....            ---------------------------------
    |                          |              |                |
IconWindow                XWindowImp      MSWindowImp    OSXWindowImp

Window and WindowImp are interfaces. IconWindow uses methods provided by Window. Window, in turn, calls the related method on imp.

The relationship between Window and WindowImp is called a Bridge.

Example: IconWindow::DrawBorder() uses Window::DrawRect(), which calls imp->DevDrawLine() which is declared in WindowImp and defined in the concrete subclasses (e.g., in class XWindowImp).

My suggestion is to read the book: Design patterns - Elements of Reusable Object-Oriented Software" (http://en.wikipedia.org/wiki/Design_Patterns) that contains the example above.

0
3

I think Java JDBC API is the best example to illustrate the user of Bridge Design Pattern. The client uses the abstraction(JDBC API) without worrying about the implementation (MYSQL, Oracle etc) that different SQL vendors provide.

To illustrate further consider that you have EmployeeDAOImpl that implements EmployeeDAO interface with save,update,delete methods. Now, this EmployeeDAOImpl will use the JDBC API (Abstraction) to perform the CRUD operations without worrying about the database used. The only thing that EmployeeDAOImpl need is the url to load the driver.

Now the crux is, the hierarchy of EmployeeDAOImpl and other DAO can vary independently. They don't have to worry about the implementation of the abstractions of JDBI API.

2
  • Please add details how JDBC implements bridge pattern.
    – Number945
    Commented May 13, 2017 at 10:18
  • @Atul Is it correct to say that the relation between EmployeeDAOImpl and JDBC is HAS-A ?
    – Victor
    Commented Feb 6, 2020 at 17:06
0

Bridge Pattern:

  • Decouple an abstraction from its implementation so that the two can vary independently;

  • Structural patterns;

  • The bridge pattern is a design pattern used in software engineering which is meant to "decouple an abstraction from its implementation so that the two can vary independently". The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes;

  • When a class varies often, the features of object-oriented programming become very useful because changes to a program's code can be made easily with minimal prior knowledge about the program. The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction. The bridge pattern can also be thought of as two layers of abstraction;

  • The bridge pattern is often confused with the adapter pattern. In fact, the bridge pattern is often implemented using the class adapter pattern;

  • Variant: The implementation can be decoupled even more by deferring the presence of the implementation to the point where the abstraction is utilized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.