Skip to content

Instantly share code, notes, and snippets.

@RimGazzeh
Last active May 21, 2020 20:16
Show Gist options
  • Save RimGazzeh/08ab5356869e01acdd05a7029c0436b7 to your computer and use it in GitHub Desktop.
Save RimGazzeh/08ab5356869e01acdd05a7029c0436b7 to your computer and use it in GitHub Desktop.
Design Patterns - Adapter Pattern
public interface CoffeeMachineInterface {
public void chooseFirstSelection();
public void chooseSecondSelection();
}
public class CoffeeTouchscreenAdapter implements CoffeeMachineInterface {
private OldCoffeeMachine mOldVendingMachine;
public CoffeeTouchscreenAdapter(OldCoffeeMachine oldVendingMachine) {
mOldVendingMachine = oldVendingMachine;
}
@Override
public void chooseFirstSelection() {
mOldVendingMachine.selectA();
}
@Override
public void chooseSecondSelection() {
mOldVendingMachine.selectB();
}
}
public class OldCoffeeMachine {
public void selectA() {
System.out.println("A selection");
}
public void selectB() {
System.out.println("B selection");
}
}
@RimGazzeh
Copy link
Author

In this example, we have a new touchscreen coffee machine and we want to connect it to the old coffee machine, I gonna use Adapter​ ​pattern​​ to connect the ​two​ ​incompatible​ ​interfaces ​by​ ​fitting​ ​between them​ ​and​ ​providing​ ​a​ ​compatible​ ​interface​ ​to​ ​both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment