Magazine
 
Structural Design Patterns

Bulb.java:

public class Bulb implements Switch {
public void switchOn() {
System.out.println(“BULB Switched ON”);
}
public void switchOff() {
System.out.println(“BULB Switched OFF”);
}}

Here is the output of the above Bulb.java program:

C:\>javac Bulb.java
C:\>java Bulb
Bulb Switched ON
Bulb Switched OFF

Similarly, let’s consider the Association and Composition of objects by which Adapter can be implemented. The above example shows how Adapter pattern works. When one interface cannot be changed and needed to suit again a cannot-be-changed client, then an adapter is used so that both the interfaces work together.

II. 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);
}

 
Oct 2007 | Java Jazz Up | 51
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78,   Download PDF