Created
June 18, 2016 12:40
-
-
Save mitulmanish/584df5d4a06626fdc64d21cea1dafda6 to your computer and use it in GitHub Desktop.
Bridge Pattern implement in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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