Definition
Decouple an abstraction or interface from its implementation so that the two can vary independently.- Decouple an abstraction from its implementation so that the two can vary independently.
- Publish interface in an inheritance hierarchy, and bury implementation in its own inheritance hierarchy.
- Beyond encapsulation, to insulation
Problem
02“Hardening of the software arteries” has occurred by using subclassing of an abstract base class to provide alternative implementations. This locks in compile-time binding between interface and implementation. The abstraction and implementation cannot be independently extended or composed.
Where to use & benefits
- Want to separate abstraction and implementation permanently
- Share an implementation among multiple objects
- Want to improve extensibility
- Hide implementation details from clients
- Related patterns include
- Abstract Factory, which can be used to create and configure a particular bridge.
- Adapter, which makes unrelated classes work together, whereas a bridge makes a clear-cut between abstraction and implementation.
The best example for this is like the electric equipments you have at home and their switches. For e.g., the switch of the fan. The switch is the interface and the actual implementation is the Running of the fan once its switched-on. Still, both the switch and the fan are independent of each other. Another switch can be plugged in for the fan and this switch can be connected to light bulb.
Let’s see how we can convert this into a software program. Switch is the interface having two functions, switchOn() and switchOff().
Here is the sample code for Switch.
Switch.java
package structural.bridge; /** * Just two methods. on and off. */ public interface Switch { | ||
// Two positions of switch. public void switchOn(); public void switchOff(); | ||
}// End of interface |
Fan.java
package structural.bridge; /** * Implement the switch for Fan */ public class Fan implements Switch { | ||
// Two positions of switch. public void switchOn() { System.out.println("FAN Switched ON"); } public void switchOff() { System.out.println("FAN Switched OFF"); } | ||
}// End of class |
And implementation as Bulb.
Bulb.java
package structural.bridge; /** * Implement the switch for Fan */ public class Bulb implements Switch { | ||
// Two positions of switch. public void switchOn() { System.out.println("BULB Switched ON"); } public void switchOff() { System.out.println("BULB Switched OFF"); } | ||
}// End of class |
Here, we can see, that the interface Switch can be implemented in different ways. Here, we can easily use Switch as an interface as it has only two functions, on and off. But, there may arise a case where some other function be added to it, like change() (change the switch). In this case, the interface will change and so, the implementations will also changed, for such cases, you should use the Switch as abstract class. This decision should be made earlier to implementation whether the interface should be interface or abstract class.
Examples
If you have a question database, you may want to develop a program to display it based on the user selection. The following is a simple example to show how to use a Bridge pattern to decouple the relationship among the objects.import java.util.*; //abstraction interface Question { public void nextQuestion(); public void priorQuestion(); public void newQuestion(String q); public void deleteQuestion(String q); public void displayQuestion(); public void displayAllQuestions(); } //implementation class QuestionManager { protected Question questDB; //instantiate it later 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(); } } //further implementation class QuestionFormat extends QuestionManager { public QuestionFormat(String catalog){ super(catalog); } public void displayAll() { System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~"); super.displayAll(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~"); } } //decoupled implementation class JavaQuestions implements Question { private List<String> questions = new ArrayList<String>(); private int current = 0; public JavaQuestions() { //load from a database and fill in the container questions.add("What is Java? "); questions.add("What is an interface? "); questions.add("What is cross-platform? "); questions.add("What is UFT-8? "); questions.add("What is abstract? "); questions.add("What is Thread? "); questions.add("What is multi-threading? "); } public void nextQuestion() { if( current <= questions.size() - 1 ) current++; } public void priorQuestion() { 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); } } } class TestBridge { public static void main(String[] args) { QuestionFormat questions = new QuestionFormat("Java Language"); questions.questDB = new JavaQuestions();//can be hooked up with other question class //questions.questDB = new CsharpQuestions(); //questions.questDB = new CplusplusQuestions(); questions.display(); questions.next(); questions.newOne("What is object? "); questions.newOne("What is reference type?"); questions.displayAll(); } } //need jdk1.5 to compileC:\ Command Prompt
C:\> javac TestBridge.java
C:\> java TestBridge
What is Java?
~~~~~~~~~~~~~~~~~~~~~~~~
Question Catalog: Java Language
What is Java?
What is an interface?
What is cross-platform?
What is UFT-8?
What is abstract?
What is Thread?
What is multi-threading?
What is object?
What is reference type?
~~~~~~~~~~~~~~~~~~~~~~~~
C:\> |
No comments:
Post a Comment