Reputation: 75
I am confused on how should I explain the bridge design pattern in java. Based on GoF's definition:
The bridge pattern is to decouple an abstraction from its implementation so that the two can vary independently.
However, I thought that we do abstractions (use of abstract classes and interfaces) to decouple the implementation from the rest of the code (since we are just declaring interfaces or abstract classes instead of the implementing class). Now, I think I am wrong on how I understood abstraction because of the bridge pattern.
What exactly is abstraction and how is it decoupled from implementation in the bridge pattern?
Upvotes: 3
Views: 806
Reputation: 131326
I thought that we do abstractions (use of abstract classes and interfaces) to decouple the implementation from the rest of the code
Your understanding is correct.
You use the bridge in corner cases where you have not only one but two (or more) abstractions that you don't want to mix together.
The GOF pattern illustrates that very well.
A window for a toolkit relies on two abstractions :
If you define a single interface : Window, you willl couple the two abstractions into a same interface and Window implementations will so couple them consequently.
If you define two interfaces : Window
(as a model/functional concept) and WindowImp
(as an OS implementation) and two distinct hierarchies : you decouple the abstractions.
Upvotes: 3
Reputation: 2083
What exactly is abstraction and how is it decoupled from implementation in the bridge pattern?
I am assuming you are familiar with the Shape and Color example used in many bridge pattern articles. For eg. here (Better go through this link if you are not familiar)
Shape and color are basically abstractions created out of a need that tomorrow you may need a Purple Rectangle or a Dodecagon with a gradient! In the article, Shape has a Color (Abstraction is depending on an abstraction). If Shape had a Red Color, it would be like Abstraction is depending on implementation (it will be coupled) and you would have to create a new class every time you need a different color.
I know my answer is insufficient but I hope its the last puzzle piece of understanding bridge pattern.
Upvotes: 0
Reputation: 43651
Abstraction in this case is used not in the sense of abstract classes etc. Abstraction is used more in a sense of higher-level idea of something whereas implementation is the specific realization of this idea.
To give an example, assume you're building toy houses. A general idea of the house is something which have walls, doors, windows and a roof. That would be the abstraction.
But you can build a house from different materials/construction sets (Lego or Duplo, paper, wood, cardboard). That would be the implementation. And in each version you'll need to know how to build walls, doors, windows, and a roof.
So you basically combine the same abstract idea of the house with different implementations. This is, in my understanding, the essense of the bridge pattern.
Upvotes: 1