Follow treslines by email clicking Here!

Wednesday, August 20, 2014

Programming Design Pattern - Bridge Pattern Applied - Best Practise

Hi there!

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){
        System.out.println("Philips Channel set: "+id);
    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) { = 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() {;}
    public void setChannel(int id) {tv.setChannel(id);}
    public void nextChannel(){
        final int currentChannelId = ((Philips)tv).getChannelId();
public class SonyControleRemoto extends ControleRemoto {
    public SonyControleRemoto(TV tv) {super(tv);}
    public void on() {tv.on();}
    public void 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());

That´s all. Hope you like it!

No comments:

Post a Comment