Last active
May 21, 2020 20:16
-
-
Save RimGazzeh/08ab5356869e01acdd05a7029c0436b7 to your computer and use it in GitHub Desktop.
Design Patterns - Adapter Pattern
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
public interface CoffeeMachineInterface { | |
public void chooseFirstSelection(); | |
public void chooseSecondSelection(); | |
} |
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
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(); | |
} | |
} |
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
public class OldCoffeeMachine { | |
public void selectA() { | |
System.out.println("A selection"); | |
} | |
public void selectB() { | |
System.out.println("B selection"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.