Design Pattern Basic Tutorial

adplus-dvertising
Bridge
Previous Home Next

The bridge Pattern is used for both implementation of the interface and vary it independently. Suppose that you have a database in which containing multiple questions and you want to display the question according the user requirement or selection. then its problem is solved only and only by help of the bridge design pattern. for doing this it is simply decouple the relationship among the object.

Benefit of the Bridge Pattern
  1. It is used in to separate abstraction and implementation permanently.
  2. Used in Share an implementation among multiple objects.
  3. It is used to improve extensibility by extending the independency.
  4. Play an important role to Hide implementation details from clients.
Usage of the Bridge Pattern

The bridge pattern is the very useful pattern. It is used when suppose we do not want the permanent binding between an abstractions and its implementation then we are used it. Bridge pattern is frequently used in those places where the changes made in the implementation does not effects to the clients.

Example

package r4r;
public interface Record {
public void nextQuestion();
public void previewousQuestion();
public void newQuestion(String q);
public void deleteQuestion(String q);
public void ShowQuestion();
public void ShowAllQuestions();

}

package r4r;
import java.util.*;
class Manager {
protected Record Rec; //instantiate it later 
public String catalog;
public Manager(String catalog) {
this.catalog = catalog;
}

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

public void prior() {
Rec.previewousQuestion();
}

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

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

public void display() {
Rec.ShowQuestion();
}

public void displayAll() {
System.out.println("Question Catalog: " + catalog);
Rec.ShowAllQuestions();
}
}
//further implementation
class Format extends Manager {

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

public void displayAll() {

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

//decoupled implementation
class JavaQuestions implements Record {

private List<String> q = new ArrayList<String>();
private int current = 0;

public JavaQuestions() {
//load from a database and fill in the container
q.add("What is Java? ");
q.add("What is an interface? ");
q.add("What is cross-platform? ");
q.add("What is UFT-8? ");
q.add("What is abstract? ");
q.add("What is Thread? ");
q.add("What is multi-threading? ");
 
}

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

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

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

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

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

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

@Override
public void ShowAllQuestions() {
// TODO Auto-generated method stub

}

@Override
public void ShowQuestion() {
// TODO Auto-generated method stub

}

@Override
public void previewousQuestion() {
// TODO Auto-generated method stub

}
}

public class bridgetest {
public static void main(String[] args) {
 
Format Fr = new Format("Java Language");

Fr.Rec = new JavaQuestions();//can be hooked up with other question class

Fr.display();
Fr.next();

Fr.newOne("What is Set Interface? ");
Fr.newOne("What is Finalizer?");

Fr.displayAll();
}
}
Previous Home Next