Ayende @ Rahien

Refunds available at head office

Design patterns in the test of time: Bridge

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.

More about this pattern.

Bridge is quite complex, mostly because it is composed of Abstraction, Refined Abstraction, Implementor and Concrete Implementor. Here is a concrete example (pun intended):

  • Abstraction: CImage
  • Refined Abstraction: CBmpImage, CJpegImage
  • Implementor: CimageImp
  • Concrete Implementor: CWinImp, COS2Imp

In general, I don’t like complex things, and of the top of my head, I can’t think of a time when I used this approach. Until such time when I can see a really good reason why I would want to do something like this, I see very little reason to bother.

Recommendation: Avoid, there are simpler options to solving those sort of problems.

Comments

Arnon Rotem-Gal-Oz
11/02/2012 12:01 PM by
Arnon Rotem-Gal-Oz

You actually see this quite a lot - only what's shown here on the CImage side all have I prefixes (in .NET) i.e. they are all interfaces...

Niv Eliyahu
11/02/2012 05:23 PM by
Niv Eliyahu

The bridge pattern come to solve problems that ioc/di solves, the bridge is the oldest way.

Niels Krijger
11/02/2012 11:53 PM by
Niels Krijger

I've seen this pattern used only used once or twice explicitly; that is, being named "Bridge". However, this pattern -like most others frankly- grow into existence somehow, particularly when you're really set for decoupling two bodies of code.

I regularly have a model project (which I can understand) implemented by a specific technology (which I don't, but with some gOOgling fiddle out in the end). I don't want the difficult technology ruin my simple view of the world (that is, I don't want technology X mess up my model). I think most who are fully dedicated to their models won't let their implementation leak into their model. Henceforth, the Bridge is created; even though the programmer has no idea what is actually is. That is why you hardly ever see it in the real world; it is often decoupled so much you don't even notice.

Kyle Szklenski
11/05/2012 04:41 PM by
Kyle Szklenski

As the occasional publisher of APIs, I have used and abused the bridge pattern over and over and over. I've used it more in C++ than in .NET, but it can be useful there, too, at times.

Marek Dec
11/21/2012 08:58 AM by
Marek Dec

Your example is somehow wrong. Actually the only Interface here is the one called CImageImp. Why does it have the Imp suffix then? Actually the CImageImp makes me think that it's an implementation of the CImage (which is not obviously not true given the uml notation you used). Then CImage cannot be a pure interface (as somebody suggests here) as it "has" a CImageImp. The quality of the wikipedia article you linked here is much better, I'd say that you read it once again and refine yours.

Comments have been closed on this topic.