What’s the difference between a Bridge, an Adapter, and a Facade? All three look approximately like this reductive example:
class ThisThing implements InterfaceA {
private InterfaceB otherThing;
String doSomething(Something argument) {
OtherShapeForSomething transformedArgument = this.somehowTransform(argument);
return this.otherThing.someOtherInterfaceForDoingSomething(transformedArgument);
}
}
Although there could be huge variation in your implementation, the essence is that there is some code that translates between two interfaces that have similar functionality but different signatures.
The difference lies in the origin of the interfaces. If we are making a thing that exposes one existing interface in the shape of another existing interface, that’s an Adapter. If we’re hiding an existing interface behind a new interface, that’s a Facade. If we’re designing two new interfaces and intend on building something to span between them, that’s a Bridge.
ThisThing |
implements InterfaceA
|
forwards to InterfaceB
|
summary |
---|---|---|---|
Adapter | an existing interface | an existing interface | fixed - fixed |
Facade | a new interface | an existing interface | custom - fixed |
Bridge | a new interface | a new interface | custom - custom |
In other words
- Adapter makes a square peg fit in a round hole
- Facade makes a thing easier to use
- Bridge makes it possible for two things to change independent of one another
Why bother distinguishing?
Technically speaking, the constraints under which we write are code make a big difference. We’re talking about three differnt things:
- adapter: writing new translation code that translates between existing things
- this is just translation
- facade: designing a new interfrace that hides an existing thing
- this is translation plus interface design
- bridge: designing two new interfaces and code that connects them
Each of these is a different sort of design activity. You can express the constraints and clarify the activity by picking the precise word: Adapter, Facade, or Bridge.
Why bother with different names?
Since they’re so similar, why not call all of these things “Adapter”?
Maybe use a full sentence instead?
You could probably get away with using any of these words interchangeably and it would be “good enough” if you add some context. You could say “let’s make a bridge so this old interface fits with our library”. Or you could say “let’s make an adapter for this because we can’t change it but it’s hard to use”. Technically, the first is an adapter (not a bridge) and the second is a facade (not an adapter), but folks would get your gist.
Express the sentence with one word: Adapter, Facade, or Bridge
However, one benefit of a pattern language is that you can express nuanced concepts concisely. We can express our sentence in a word if we learn to distinguish between Adapter, Facade, and Bridge.