Implement Another Version of Adapter Pattern

master
Hammy 4 years ago
parent 7b5de89dd4
commit 017a22e9fc

@ -0,0 +1,8 @@
package adapter.animal;
public interface Bird {
void fly();
void makeSound();
}

@ -0,0 +1,16 @@
package adapter.animal;
public class BirdAdapter implements ToyDuck {
private final Bird bird;
public BirdAdapter(Bird bird) {
this.bird = bird;
}
@Override
public void squeak() {
bird.makeSound();
}
}

@ -0,0 +1,21 @@
package adapter.animal;
public class Main {
public static void main(String[] args) {
Bird sparrow = new Sparrow();
ToyDuck plasticToyDuck = new PlasticToyDuck();
System.out.println("Sparrow...");
sparrow.fly();
sparrow.makeSound();
System.out.println("\nPlasticToyDuck...");
plasticToyDuck.squeak();
System.out.println("\nBirdAdapter...");
ToyDuck birdAdapter = new BirdAdapter(sparrow);
birdAdapter.squeak();
}
}

@ -0,0 +1,9 @@
package adapter.animal;
public class PlasticToyDuck implements ToyDuck {
@Override
public void squeak() {
System.out.println("Squeak");
}
}

@ -0,0 +1,14 @@
package adapter.animal;
public class Sparrow implements Bird {
@Override
public void fly() {
System.out.println("Sparrow is Flying");
}
@Override
public void makeSound() {
System.out.println("*Sparrow Noises*");
}
}

@ -0,0 +1,6 @@
package adapter.animal;
public interface ToyDuck {
void squeak();
}
Loading…
Cancel
Save