Today i'm gonna show the Bridge design pattern in action. The Bridge design pattern is a very useful programming design pattern whenever you need to be able to vary abstractions and implementations of structures.
I'm assuming here you already know the concepts and i'll be focusing on practise. The example i will provide is a nice way to show it how it could looks like. You can always come back here, take it, adapt it and use it in your applictions as you may need. So be sure you bookmark it or join the group here on the right side of this post subscribing it.
First of all, let's take a look at the UML diagram of it. After that we will take the analogy for our example.
The UML Diagram of the Bridge Pattern
Pay close attention, because once you understand that, everything will become clear and simple to understand. That's the reason I'm putting always the UML first. That way you'll get an eye for it with the time.
The example
In our example we will see, how we could separete the remote control from the TV, so we can vary both sides. (The abstraction[remote controls] and the implemention[TV's] of it) This way we will be able to implement platforms dependent TV's and remote controls.The Bridge
This is the common interface that acts between two object structures (the bridge). The TV types are the structure of different clients that can have different remote controls.public interface TV { public void on(); public void off(); public void setChannel(int id); } public class Sony implements TV { public void on() {System.out.println("Sony TV on!");} public void off() {System.out.println("Sony TV off!");} public void setChannel(int id){System.out.println("Sony Channel set: "+id);} } public class Philips implements TV { private int channelId=0;//SPECIFIC FEATURE FROM PHILIPS public void on() {System.out.println("Philips TV on!");} public void off() {System.out.println("Philips TV off!");} public void setChannel(int id){ this.channelId=id; System.out.println("Philips Channel set: "+id); } // SPECIFIC IMPLEMENTATION FROM PHILIPS public int getChannelId() {return channelId;} }
The Remote Control
Each TV communicates over different remote controls and each of them could have more, less or different functions.public abstract class ControleRemoto { protected TV tv; public ControleRemoto(TV tv) {this.tv = tv;} public abstract void on(); public abstract void off(); public abstract void setChannel(int id); } public class PhilipsControleRemoto extends ControleRemoto { public PhilipsControleRemoto(TV tv) {super(tv);} public void on() {tv.on();} public void off() {tv.off();} public void setChannel(int id) {tv.setChannel(id);} // SPECIFIC IMPLEMENTATION FOR PHILIPS public void nextChannel(){ final int currentChannelId = ((Philips)tv).getChannelId(); tv.setChannel(currentChannelId+1); } } public class SonyControleRemoto extends ControleRemoto { public SonyControleRemoto(TV tv) {super(tv);} public void on() {tv.on();} public void off() {tv.off();} public void setChannel(int id) {tv.setChannel(id);} }
The Test
Here we can see how the clients (TV's) communicate over the bridges with the different remote controls. With this concept, both, TV's and Remote Control's, can vary.public class Client { public static void main(String[] args) { final ControleRemoto philips = new PhilipsControleRemoto(new Philips()); final ControleRemoto sony = new SonyControleRemoto(new Sony()); sony.setChannel(3); // PHILIPS HAS MORE FUNCTIONALITIES philips.setChannel(18); ((PhilipsControleRemoto)philips).nextChannel(); } }
That´s all. Hope you like it!
😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO 😱👇
Be sure to read, it will change your life!
Show your work by Austin Kleon: https://amzn.to/34NVmwx
This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practices: https://amzn.to/30WQSm2
Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv
This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp
Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy
😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO 😱👆