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.