Last active
July 7, 2020 09:50
-
-
Save Tiorzfk/fe96bf137ee4cde8658e0522b7055a80 to your computer and use it in GitHub Desktop.
(Adapter) Langkah Pembuatan : 1. Interface Bird.java dan ToyDuck.java 2. Class Sparrow.java dan PlasticToyDuck.java 3. BirdAdapter.java 4. AdapterClient.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
public class AdapterClient { | |
public static void main(String[] args) { | |
Sparrow sparrow = new Sparrow(); | |
PlasticToyDuck toyDuck = new PlasticToyDuck(); | |
// Bungkus bird dengan birdAdapter sehingga bertingkah laku seperti toy duck | |
ToyDuck birdAdapter = new BirdAdapter(sparrow); | |
System.out.println("Sparrow..."); | |
sparrow.fly(); | |
sparrow.makeSound(); | |
System.out.println("ToyDuck..."); | |
toyDuck.squeak(); | |
// bird bertingkah laku seperti toy duck | |
System.out.println("BirdAdapter..."); | |
birdAdapter.squeak(); | |
} | |
} |
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 Bird { | |
public void fly(); | |
public void makeSound(); | |
} |
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 BirdAdapter implements ToyDuck { | |
Bird bird; | |
public BirdAdapter(Bird bird) { | |
this.bird = bird; | |
} | |
@Override | |
public void squeak() { | |
bird.makeSound(); | |
} | |
} |
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 PlasticToyDuck implements ToyDuck{ | |
@Override | |
public void squeak() { | |
System.out.printf("Squeak"); | |
} | |
} |
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 Sparrow implements Bird { | |
@Override | |
public void fly() { | |
System.out.printf("Flying"); | |
} | |
@Override | |
public void makeSound() { | |
System.out.printf("Chirp chirp"); | |
} | |
} |
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 ToyDuck { | |
public void squeak(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment