From 017a22e9fc69ebd88db31d7c291de4dfb42b9d52 Mon Sep 17 00:00:00 2001 From: Hammy Date: Mon, 14 Dec 2020 20:33:14 +0000 Subject: [PATCH] Implement Another Version of Adapter Pattern --- Design Patterns/src/adapter/animal/Bird.java | 8 +++++++ .../src/adapter/animal/BirdAdapter.java | 16 ++++++++++++++ Design Patterns/src/adapter/animal/Main.java | 21 +++++++++++++++++++ .../src/adapter/animal/PlasticToyDuck.java | 9 ++++++++ .../src/adapter/animal/Sparrow.java | 14 +++++++++++++ .../src/adapter/animal/ToyDuck.java | 6 ++++++ 6 files changed, 74 insertions(+) create mode 100644 Design Patterns/src/adapter/animal/Bird.java create mode 100644 Design Patterns/src/adapter/animal/BirdAdapter.java create mode 100644 Design Patterns/src/adapter/animal/Main.java create mode 100644 Design Patterns/src/adapter/animal/PlasticToyDuck.java create mode 100644 Design Patterns/src/adapter/animal/Sparrow.java create mode 100644 Design Patterns/src/adapter/animal/ToyDuck.java diff --git a/Design Patterns/src/adapter/animal/Bird.java b/Design Patterns/src/adapter/animal/Bird.java new file mode 100644 index 0000000..d5ee07e --- /dev/null +++ b/Design Patterns/src/adapter/animal/Bird.java @@ -0,0 +1,8 @@ +package adapter.animal; + +public interface Bird { + + void fly(); + + void makeSound(); +} diff --git a/Design Patterns/src/adapter/animal/BirdAdapter.java b/Design Patterns/src/adapter/animal/BirdAdapter.java new file mode 100644 index 0000000..94d2bec --- /dev/null +++ b/Design Patterns/src/adapter/animal/BirdAdapter.java @@ -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(); + } + +} diff --git a/Design Patterns/src/adapter/animal/Main.java b/Design Patterns/src/adapter/animal/Main.java new file mode 100644 index 0000000..2f9fa9e --- /dev/null +++ b/Design Patterns/src/adapter/animal/Main.java @@ -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(); + + } +} diff --git a/Design Patterns/src/adapter/animal/PlasticToyDuck.java b/Design Patterns/src/adapter/animal/PlasticToyDuck.java new file mode 100644 index 0000000..e68262d --- /dev/null +++ b/Design Patterns/src/adapter/animal/PlasticToyDuck.java @@ -0,0 +1,9 @@ +package adapter.animal; + +public class PlasticToyDuck implements ToyDuck { + + @Override + public void squeak() { + System.out.println("Squeak"); + } +} diff --git a/Design Patterns/src/adapter/animal/Sparrow.java b/Design Patterns/src/adapter/animal/Sparrow.java new file mode 100644 index 0000000..fc607bd --- /dev/null +++ b/Design Patterns/src/adapter/animal/Sparrow.java @@ -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*"); + } +} diff --git a/Design Patterns/src/adapter/animal/ToyDuck.java b/Design Patterns/src/adapter/animal/ToyDuck.java new file mode 100644 index 0000000..cc27671 --- /dev/null +++ b/Design Patterns/src/adapter/animal/ToyDuck.java @@ -0,0 +1,6 @@ +package adapter.animal; + +public interface ToyDuck { + + void squeak(); +}