Bridge Pattern

A Bridge Pattern says that just “decouple the functional abstraction from the implementation so that the two can vary independently”.

The Bridge Pattern is also known as Handle or Body.

Advantage of Bridge Pattern

  • It enables the separation of implementation from the interface.
  • It improves the extensibility.
  • It allows the hiding of implementation details from the client.

Usage of Bridge Pattern

  • When you don’t want a permanent binding between the functional abstraction and its implementation.
  • When both the functional abstraction and its implementation need to extended using sub-classes.
  • It is mostly used in those places where changes are made in the implementation does not affect the clients.

Example of Bridge Pattern

The UML given below describes the example of bridge pattern.

ML for Bridge Pattern:

UML of Bridge Pattern

Implementation of above UML:

Step 1

Create a Question interface that provides the navigation from one question to another or vice-versa.

// this is the Question interface.

public interface Question {

public void nextQuestion();

public void previousQuestion();

public void newQuestion(String q);

public void deleteQuestion(String q);

public void displayQuestion();

public void displayAllQuestions();

}

// End of the Question interface.

Step 2

Create a JavaQuestions implementation class that will implement Question interface.

// this is the JavaQuestions class.

import java.util.ArrayList;

import java.util.List;

public class JavaQuestions implements Question {

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

privateintcurrent = 0;

public JavaQuestions() {

questions.add(“What is class? “);

questions.add(“What is interface? “);

questions.add(“What is abstraction? “);

questions.add(“How multiple polymorphism is achieved in java? “);

questions.add(“How many types of exceptionhandling are there in java? “);

questions.add(“Define the keyword final forvariable, method, and class in java? “);

questions.add(“What is abstract class? “);

questions.add(“What is multi-threading? “);

}

public void nextQuestion() {

if (current <= questions.size() 1)

current++;

System.out.print(current);

}

public void previousQuestion() {

if (current > 0)

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);

}

}

}// End of the JavaQuestions class.

Step 3

Create a QuestionManager class that will use Question interface which will act as a bridge..

// this is the QuestionManager class.

public class QuestionManager {

protectedQuestion q;

public String catalog;

public QuestionManager(String catalog) {

this.catalog = catalog;

}

publicvoidnext() {

q.nextQuestion();

}

publicvoidprevious() {

q.previousQuestion();

}

publicvoidnewOne(String quest) {

q.newQuestion(quest);

}

publicvoiddelete(String quest) {

q.deleteQuestion(quest);

}

publicvoiddisplay() {

q.displayQuestion();

}

publicvoiddisplayAll() {

System.out.println(“Question Paper: ” + catalog);

q.displayAllQuestions();

}

}// End of the QuestionManager class.

Step 4

Create a QuestionFormat class that will extend the QuestionManager class

// this is the QuestionFormat class.

public class QuestionFormat extends QuestionManager {

public QuestionFormat(String catalog) {

super(catalog);

}

publicvoiddisplayAll() {

System.out.println(“\n———————————————————“);

super.displayAll();

System.out.println(“———————————————————–“);

}

}// End of the QuestionFormat class.

Step 5

Create a BridgePatternDemo class.

// this is the BridgePatternDemo class.

public class BridgePatternDemo {

publicstaticvoidmain(String[] args) {

QuestionFormat questions = new QuestionFormat(“Java Programming Language”);

questions.q = new JavaQuestions();

questions.delete(“what is class?”);

questions.display();

questions.newOne(“What is inheritance? “);

questions.newOne(“How many types of inheritance are there in java?”);

questions.displayAll();

}

}// End of the BridgePatternDemo class.

OUTPUT:

What is interface?

——————————————————————–

Question Paper: Java Programming Language

What is class?

What is interface?

What is abstraction?

How multiple polymorphism is achieved in java?

How many types of exceptionhandling are there in java?

Define the keyword final forvariable, method, and class in java?

What is abstract class?

What is multithreading?

What is inheritance?

How many types of inheritance are there in java?

———————————————-———————–

Popular posts from this blog

How to change column name in Dataframe and selection of few columns in Dataframe using Pyspark with example

What is Garbage collection in Spark and its impact and resolution

Window function in PySpark with Joins example using 2 Dataframes (inner join)