File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Adapter Pattern Vs. Bridge Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Adapter Pattern Vs. Bridge Pattern" Watch "Adapter Pattern Vs. Bridge Pattern" New topic
Author

Adapter Pattern Vs. Bridge Pattern

sriram sundararajan
Ranch Hand

Joined: Apr 08, 2003
Posts: 43
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
sriram sundararajan
Ranch Hand

Joined: Apr 08, 2003
Posts: 43
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

Joined: Aug 16, 2013
Posts: 1
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adapter Pattern Vs. Bridge Pattern
 
Similar Threads
HAS-A relationship?
how to name and save the Television interface
Switch Statement
HAS-A relationship?
interface example