This post may contain affiliate links. Please see my full disclosure policy for details.
I originally wrote this post in December of 2009. Being that it is quite popular I decided to migrate it to my current blog. As I migrated this article I noticed that the code link was no longer available. If you are visiting this and the code is not there my apologies. I will have the post there soon. In the meantime you can visit wikipedia for an example https://en.wikipedia.org/wiki/Bridge_pattern
As I was preparing for an upcoming presentation on the topic of Design Patterns, I was studying through the different patterns and could not differentiate between the Bridge Pattern and the Adapter Pattern. Right off the bat both of those patterns fall into the category of structural patterns. Both of those patterns allow us to take advantage of the Liskov Substitution Principle. Both patterns allow us to swap out implementations.
It seems that I am not alone in getting confused in the distinction between these two patterns. A simple google search with the phrase “bridge and adapter pattern” returns quite a few results. Here is an article in code project that illustrates the difference to a certain degree.
What came to mind in trying to determine the pattern required is the forces at work. This something that Steve Tockey pointed out when determining what pattern to use. He said basically that an engineer does not wake up one day and say “I think I will build a suspension bridge”. The engineer must take into account the forces (tension, compression, bending, torsion, shear) that the bridge is most suitable for based on the architectural requirements. In other words, he must take into account the physical requirements of the bridge.
Taking the forces into account, made me think about what exactly each of the two patterns was trying to solve. What is the problem that I am trying to solve? The bridge pattern is simply separating abstraction from implementation. This separation allows us to swap out different implementations at runtime. This is the basic premise of an interface or an abstract class. On the other hand, the adapter pattern is trying to adapt an interface to an implementation.
In the case of the bridge pattern, this may be a simple case of different implementations. In other words, two classes that happen to implement the same interface. With the bridge pattern, the programmer will usually have ownership of the interface definition as well as the concrete class.
In the case of the adapter pattern, the programmer will usually not have ownership of the concrete class that it is adapting to. Additionally, the programmer may have to do some conversion of data types, converting from more parameters to fewer parameters or vise-versa. We are actually adopting one concrete class(this may be a class or a static call) to an abstraction.
Take the following code for an example of an adapter for a graphics API. Although we are showing the GraphicsAPI code lets assume that we only have access to the binary assembly (DLL). This means that applying an interface to the Graphics API is out of the question. Furthermore, even if the code was available it would still not be feasible since the methods are static. Even in some cases where we might own the code, it might not be feasible to modify the existing code since that would result in breaking existing code depends on it.
Now in our client code, we can call the GraphicsAPI methods directly – this what most beginner coders would do. Later down the road in the maintenance of the code we might discover that we want to swap out the GraphicsAPI for some other API but since we made direct calls everywhere, we will have to change them to call the new API. If you are doing TDD you will come to this realization much earlier.
To avoid this problem altogether we can create an adapter to the API and use the adapter instead. This will enable us to later swap out the implementations or at least modify the code in one place where the API calls are made.
In the example I provide below I combine the bridge and adapter pattern to make the code more loosely coupled. Keep in mind that the bridge pattern does not require the adapter pattern, but in many cases, the adapter pattern will need the bridge pattern. In my opinion, the bridge pattern is what the whole premise of inversion of control is built on. This is basically the concept that you can swap out implementations by using abstraction.
As a disclaimer, I will say that this explanation is by no means complete. As always, I encourage each of you to research and discover these patterns for yourself