Design Patterns : Bridge
Updated: Jul 12, 2021
As always here is the link to the GitHub repo :)
If this post has helped you feel free to like, comment and share.
So... today's post is all about the bridge pattern. When to use it and how does it work.
First of, it is used for when you want to decouple an abstraction from it's implementation such that the two can vary independently. Cool stuff. Here is a diagram of how this pattern works:
For our example, what we want to do is to have a political speaker manager. When someone gets called they say their bit. We don't know who they are but, we know that they have something to say.
Abstraction (SpeakerCoordinator): This class is going to be the one we call when we want one of our speakers to talk. It holds a reference to a speaker and nothing more.
Implementation (FirstSpeaker, SecondSpeaker): These are our implementations, basically where the processing and computing lives. Their "Speak" method is called by the Abstraction.
Here is an example of how this all ties in together. As you can see we can easily decouple our objects making our code more flexible.
I hope that this has help you to understand the bridge pattern better.