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.
The bridge pattern is a design pattern used in software engineering that is meant to "decouple an abstraction from its implementation so that the two can vary independently", introduced by the Gang of Four.
Three Types of Design Patterns (Behavioral, Creational, Structural) Distinguish between Behavioral, Creational, and Structural Design Patterns.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With