Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is benefit of Bridge Pattern [closed]

When I want to learn something new, I ask myself, what I lost when I don't learn that thing. I am going to learn some design patterns and everything is good. But when I arrived to Bridge Design Pattern I fell in problem. Really I can not imagine when to use this design pattern. And I know there is another links in google and stackoverflow like this.

But can anyone say that, what we lost if we forget Bridge Design Pattern and skip try this pattern?

like image 825
Seyed Morteza Mousavi Avatar asked Aug 14 '13 10:08

Seyed Morteza Mousavi


People also ask

What is the advantage of Bridge pattern?

Advantage of Bridge PatternIt enables the separation of implementation from the interface. It improves the extensibility. It allows the hiding of implementation details from the client.

What is the purpose of Bridge pattern?

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.

What problem does Bridge pattern solve?

Bridge pattern decouple an abstraction from its implementation so that the two can vary independently. It is used mainly for implementing platform independence features.

Where are bridge design patterns used?

Bridge design pattern can be used when both abstraction and implementation can have different hierarchies independently and we want to hide the implementation from the client application.


1 Answers

The Bridge pattern is simply noticing a couple of converging responsibilities and separating them. I'll use The Gang of Four (TGF)'s example because I think its a really good one:

You have a Window interface, with two subclasses: XWindow (based on the X Window Manager) and PMWindow (based on IBM's Presentation Manager (PM) Window Manager... which I've never heard of).

ie:

interface Window {}
class XWindow : Window {}
class PMWindow : Window {}

The problem with continuing in our traditional inheritance approach is that if you specialize Window on an aspect other than its platform dependence (ie, you have some responsibility that is orthogonal to the one you created the inheritance tree to support), you need to use the bridge pattern, otherwise your class hierarchy will grow in depth geometrically. I think a good way to think of bridge as a combination of inheritance and composition.

That's pretty wordy. Going back to the TGF example: what if you want an IconWindow and a TransientWindow (something like a glass pane). The concept of "Icon vs Transient" and "PM vs X" are two orthogonal ideas, but they're both trying to get on the same inheritance tree. If don't use the bridge pattern, what you'll have to do is create two new interfaces inheriting from the first, and a slew of classes beneath them:

interface Window {}
class XWindow : Window {}
class PMWindow : Window {}
interface IconWindow : Window {}
class XIconWindow : XWindow, IconWindow {}
class PMIconWindow : PMWindow, IconWindow {}
interface TransientWindow : Window {}
class XTransientWIndow : XWindow, TransientWindow {}
class PMTransientWindow : PMWindow, TransientWindow {}

With the bridge pattern you would separate those two responsibilities onto two inheritance trees:

interface Window {}
class IconWindow : Window {} //this class...
class TransientWindow : Window {} //and this one will keep a private reference to aWindowImp
interface WindowImp: Window {}
class XWindowImp : WindowImp {}
class PMWindowImp : WindowImp {}

Much cleaner, much better responsibility segregation, and much easier to write and consume!

I believe that this design problem and the oddities of even the bridge object tree were actually some of the issues driving the design of Mix-ins in Scala. Using C++'s multiple inheritance, you'd statically couple any implementations to their windowing system. In other words, you'd have the same number of types as the non-bridge pattern but they'd probably be empty classes and you can of course refer to them by the abstraction, which makes it fairly easy to consume.

like image 143
Groostav Avatar answered Sep 19 '22 09:09

Groostav