• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adapter Pattern Vs. Bridge Pattern

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Does this tiny ad smell okay to you?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic