dcsimg
Adapter Pattern Vs. Bridge Pattern
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sriram_Sundararajan
Posted On:   Wednesday, December 27, 2006 01:05 PM

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{    More>>

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("
Object Adapter pattern results....
");

objFanAda.start();

objFanAda.stop();




ClassFanAdapter classFanAda = new ClassFanAdapter();

System.out.println("
Class Adapter pattern results....
");

classFanAda.start();

classFanAda.stop();

}



}


regards

sriram.

   <<Less

Re: Adapter Pattern Vs. Bridge Pattern

Posted By:   Anonymous  
Posted On:   Friday, January 19, 2007 05:54 PM

Adapter pattern is for converting the existing interfaces to a new interface to achieve compatibility and reusability of the unrelated classes in one application. It is also know as wrapper pattern.

Check this link for more details


http://www.java-interview.com/Adapter_Pattern.html





Where as bridge pattern decouples an abstraction or interface from its implementation so that the two can vary independently.

Check this link for more details


http://www.java-interview.com/Bridge_Pattern.html

About | Sitemap | Contact