A Brief Introduction To Bridge Design Pattern In Java

Posted By : Pavan Kumar | 29-Apr-2018

Bridge design pattern:- Bridge design pattern comes under the Behavioural design patterns of java. it is used when we need to decouple the abstraction from the implementation so that they can be independent.

Usage:-

  • It's very useful when you don't want the permanent binding between functional abstraction and implementation.
  • When we don't want to affect changes of implementation into other/client.

Let's understand it through practical implementation.

Here we will Use the Player interface that will provide the navigation from one to another.

Player.java

public interface Player {
    public void nextPlayer();
    public void previousPlayer();
    public void newPlayer(String q);
    public void deletePlayer(String q);
    public void displayPlayer();
    public void displayAllPlayers();
}

Now we will create a CricketPlayers class that will implement Player interface.

CricketPlayers.java:-

public class CricketPlayers implements Player {
    private List players = new ArrayList();
    private int current = 0;

    public CricketPlayers() {
        players.add("Rohit Sharma");
        players.add("Virat Kohli");
        players.add("M.S. Dhoni");
        players.add("Suresh Raina");

    }

    public void nextPlayer() {
        if( current <= players.size()-1 )
            current++;
        System.out.print(current);
    }

    public void previousPlayer() {
        if( current > 0 )
            current--;
    }

    public void newPlayer(String q) {
        players.add(q);
    }

    public void deletePlayer(String q) {
        players.remove(q);
    }

    public void displayPlayer() {
        System.out.println( players.get(current) );
    }

    public void displayAllPlayers() {
        for (String player : players) {
            System.out.println(player);
        }
    }
}

Now we will create a PlayerManager class that will use Player interface which act as a bridge.

PlayerManager.java

public class PlayerManager {
    protected Player player;
    public String catalog;
    public PlayerManager(String catalog) {
        this.catalog=catalog;
    }
    public void next() {
        player.nextPlayer();
    }
    public void previous() {
        player.previousPlayer();
    }
    public void newOne(String str) {
        player.newPlayer(str);
    }
    public void delete(String str) {
        player.deletePlayer(str);
    }
    public void display() {
        player.displayPlayer();
    }
    public void displayAllPlayers() {
        System.out.println("Player: " + catalog);
        player.displayAllPlayers();
    }
}

Now we will create a PlayerFormat class that will extends the PlayerManager class.

PlayerFormat.java

public class PlayerFormat extends PlayerManager {
    public PlayerFormat(String catalog) {
        super(catalog);
    }

    public void displayAllPlayers() {
        System.out.println("\n---------------------------------------------------------");
        super.displayAllPlayers();
        System.out.println("-----------------------------------------------------------");
    }
}

Now we will create the BridgePatternDemo class which will test the functionality.

BridgePatternDemo.java

public class BridgePatternDemo {
    public static void main(String[] args) {
        PlayerFormat players = new PlayerFormat("Cricket Player");
        players.player = new CricketPlayers();
        players.deletePlayer("Rohit Sharma");
        players.display();
        players.newPlayer("Virat Kohli ");

        players.newPlayer("M.S. Dhoni");
        players.displayAllPlayers();
    }
}

 

About Author

Author Image
Pavan Kumar

Pavan is a bright Java developer. He is a learner by heart and has a passion and profile to adapt various technologies.

Request for Proposal

Name is required

Comment is required

Sending message..