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 :
Develop another class QuestionManager implementing the Question.java interface :
Develop another class QuestionFormat extending the QuestionManager class
Develop another class JavaQuestions
Develop another class TestBridge.
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.
Rs. 20,000 US$ 300
Today: Rs. 10,000 US$150
Course Duration: 30 hrs
Posted on: December 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles