Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adapter Pattern Vs. Bridge Pattern

 
sriram sundararajan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would like to know the difference between Adapter pattern and the Bridge pattern. Both seems to be doing a same job for me, is there any conceptual difference between the two? Below is the example for the two Adapter pattern types (Object adapter and Class adapter). Using Bridge pattern how can i communicate between switch and the Fan interface. Any help or suggession will be appreaciated.


interface Switch {
public void switchOn(String electricAppliance);
public void switchOff(String electricAppliance);
}

interface Fan {
public void stop();
public void start();
}

class mySwitch implements Switch{
public void switchOn(String electricAppliance){
System.out.println("Give Electricity to the "+electricAppliance+" Motor");
}
public void switchOff(String electricAppliance){
System.out.println("Disconnect the Electricity from the "+electricAppliance+" Motor");
}
}



class ObjectFanAdapter implements Fan{
private mySwitch swit = null;
private String applicanceName = "Fan";
ObjectFanAdapter(mySwitch swit){
this.swit = swit;
}

public void stop(){
swit.switchOff(applicanceName);
}
public void start(){
swit.switchOn(applicanceName);
}

}

class ClassFanAdapter extends mySwitch implements Fan{
ClassFanAdapter(){}
private String applicanceName = "Fan";

public void stop(){
this.switchOff(applicanceName);
}

public void start(){
this.switchOn(applicanceName);
}
}

public class MyAdapterPattern
{
public static void main(String ar[]){
ObjectFanAdapter objFanAda = new ObjectFanAdapter(new mySwitch());
System.out.println("\nObject Adapter pattern results....\n");
objFanAda.start();
objFanAda.stop();

ClassFanAdapter classFanAda = new ClassFanAdapter();
System.out.println("\nClass Adapter pattern results....\n");
classFanAda.start();
classFanAda.stop();
}

}

regards
sriram.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can tell, the Bridge pattern isn't applicable to that situation at all.

http://today.java.net/pub/a/today/2004/10/29/patterns.html has a good explanation of the Bridge pattern.
 
sriram sundararajan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for your suggestion. According to the Bridge pattern derivation
"The bridge pattern is a design pattern used in software engineering which is meant to "decouple an abstraction from its implementation so that the two can vary independently" (Gamma et. al.). The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes."

So in this case i took a totally independent interface called Fan and Switch. The reason why i wanted to go for this pattern is I can have any type of Fan and Switch, but it should independent of each other. Let as consider, ceiling fan, Table fan, black color switch and white color switch. At given point of time i have to operate the fan by using either type of the switch. So based on this reason only i went for Bridge Pattern.

Please refer the URL for bridge pattern "http://en.wikipedia.org/wiki/Bridge_pattern", and they explained that by using a UML diagram. I am not getting that diagram ( :-) probably because of this i wouldn't understand the concept).

Your thoughts/ suggestion will be appreciated.

regards
sri.
 
Basma Alkerm
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think they have no big different . but they target two different situations:

"The Adapter pattern is geared toward making unrelated classes work together.
It is usually applied to systems after they're designed. Bridge, on the other
hand, is used up-front in a design to let abstractions and implementations vary
independently."


As the GOF book say's at the "Bridge pattern" section, "Related patterns" part.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic