Skip to content

Instantly share code, notes, and snippets.

@Tiorzfk
Last active July 7, 2020 09:50
Show Gist options
  • Save Tiorzfk/fe96bf137ee4cde8658e0522b7055a80 to your computer and use it in GitHub Desktop.
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
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();
}
}
public interface Bird {
public void fly();
public void makeSound();
}
public class BirdAdapter implements ToyDuck {
Bird bird;
public BirdAdapter(Bird bird) {
this.bird = bird;
}
@Override
public void squeak() {
bird.makeSound();
}
}
public class PlasticToyDuck implements ToyDuck{
@Override
public void squeak() {
System.out.printf("Squeak");
}
}
public class Sparrow implements Bird {
@Override
public void fly() {
System.out.printf("Flying");
}
@Override
public void makeSound() {
System.out.printf("Chirp chirp");
}
}
public interface ToyDuck {
public void squeak();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment