Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Tutorials
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.