Skip to content

Instantly share code, notes, and snippets.

@mitulmanish
Created June 18, 2016 12:40
Show Gist options
  • Save mitulmanish/584df5d4a06626fdc64d21cea1dafda6 to your computer and use it in GitHub Desktop.
Save mitulmanish/584df5d4a06626fdc64d21cea1dafda6 to your computer and use it in GitHub Desktop.
Bridge Pattern implement in Java
interface Switch {
void turnOn();
}
interface Appliance {
void run();
}
class TV implements Appliance {
@Override
public void run() {
System.out.println("TV is running");
}
}
class VaccumCleaner implements Appliance {
@Override
public void run() {
System.out.println("Vaccum Cleaner is running");
}
}
class RemoteControl implements Switch {
Appliance appliance;
public void setAppliance(Appliance appliance) {
this.appliance = appliance;
}
@Override
public void turnOn() {
appliance.run();
}
}
public class Main {
public static void main(String[] args) {
// write your code here
List<Appliance> applianceList = new ArrayList<>(Arrays.asList(new TV(), new VaccumCleaner()));
RemoteControl remoteControl = new RemoteControl();
for(Appliance appliance: applianceList) {
remoteControl.setAppliance(appliance);
remoteControl.turnOn();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment