Bridge Pattern

Builder pattern provides independence to the interface from its implementation. It provides flexibility to both to vary independently.

Bridge Pattern

Bridge Pattern

     

Builder pattern provides independence to the interface from its implementation. It provides flexibility to both to vary independently. Suppose we have a database containing multiple questions and we want to display the questions on the basis of the user selections. Then such type of problems can be solved with the Bridge Design Pattern. It does that simply by decoupling the relationship among the objects. 

Benefits: It separates the abstraction from the implementation details. Inheritance tightly couples the abstraction with the implementations at the compile time, However the Bridge pattern hides the implementation details, improves the extensibility, shares an implementation among multiple objects. It reduces the number of subclasses, sometimes use of pure inheritance increases the number of subclasses. It also improves the extensibility by extending independency between the abstraction and the implementation.

Usage: It is used in such situation if you do not want the permanent binding between an abstraction and its implementation. It is  frequently used in those places where changes made in the implementation does not effects the clients. It can be used to fulfill such requirements where the abstractions and the implementations needs to be extensible.

Now lets take an example:
First develop a Question.java interface :

Question.java :

interface Question {

public void nextQuestion();
public void priorQuestion();
public void newQuestion(String q);
public void deleteQuestion(String q);
public void displayQuestion();
public void displayAllQuestions();

}

Develop another class QuestionManager implementing the Question.java interface : 

class QuestionManager {

protected Question questDB;
public String catalog;

public QuestionManager(String catalog) {
this.catalog = catalog;
}

public void next() {
questDB.nextQuestion();
}

public void prior() {
questDB.priorQuestion();
}

public void newOne(String quest) {
questDB.newQuestion(quest);
}

public void delete(String quest) {
questDB.deleteQuestion(quest);
}

public void display() {
questDB.displayQuestion();
}

public void displayAll() {
System.out.println("Question Catalog: " + catalog);
questDB.displayAllQuestions();
}

}

Develop another class QuestionFormat extending the QuestionManager class

QuestionFormat.java

class QuestionFormat extends QuestionManager {

public QuestionFormat(String catalog){
super(catalog);
}

public void displayAll() {

System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~");
super.displayAll();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
}

}

Develop another class JavaQuestions implementing the "Question" interface : 
JavaQuestions.java:

import java.util.*;

class JavaQuestions implements Question {

private List <String> questions = new ArrayList<String>();

private int current = 0;

public JavaQuestions() {
questions.add("What is Java? ");
questions.add("What is marker interface? ");
questions.add("What is cross-platform? ");
questions.add("How multiple polymorphism 
   is achieved in java? "
);
questions.add("How many types of exception
   handling are there in java? "
);
questions.add("Define the keyword final for 
   variable, method, and class
   in java? "
);
questions.add("What is multi-tasking? ");
questions.add("What is multi-threading? ");

}

public void nextQuestion() {
if( current <= questions.size() - )
current++;
}

public void priorQuestion() {
if( current > )
current--;
}

public void newQuestion(String quest) {
questions.add(quest);
}

public void deleteQuestion(String quest) {
questions.remove(quest);
}

public void displayQuestion() {
System.out.println( questions.get(current) );
}

public void displayAllQuestions() {
for (String quest : questions) {
System.out.println(quest);
}
}

}

Develop another class TestBridge.

TestBridge.java 

class TestBridge {

public static void main(String[] args) {

QuestionFormat questions = new QuestionFormat
"Java Language");

questions.questDB = new JavaQuestions();
// questions.questDB = new CsharpQuestions();
// questions.questDB = new CplusplusQuestions();

questions.display();
questions.next();

questions.newOne("What is polymorphism? ");
questions.newOne("How many types of polymorphism 
  are there in java?"
);

questions.displayAll();
}

}

Here is the output of the above program:

C:\ Command Prompt 
C:\> javac TestBridge.java
C:\> java TestBridge
What is Java? 

~~~~~~~~~~~~~~~~~~~~~~~~
Question Catalog: Java Language
What is Java? 
What is marker interface? 
What is cross-platform? 
How multiple polymorphism is achieved in java? 
How many types of exception handling are there in java? 
Define the keyword final for variable, method, and class in java? 
What is multi-tasking? 
What is multi-threading? 
What is polymorphism? 
How many types of polymorphism are there in java?
~~~~~~~~~~~~~~~~~~~~~~~~

The above example tries to show how the Bridge pattern pattern decouples the interface from its implementation. One can easily notice that the class JavaQuestion can be launched independently working as an independent system.