From 8383411a5472437562a1cb683c85902d4b812cde Mon Sep 17 00:00:00 2001 From: Hammy Date: Fri, 25 Dec 2020 02:16:29 +0000 Subject: [PATCH] Implement Facade Pattern --- Design Patterns/src/facade/Amplifier.java | 46 ++++++++++ Design Patterns/src/facade/CDPlayer.java | 57 +++++++++++++ Design Patterns/src/facade/DVDPlayer.java | 64 ++++++++++++++ .../src/facade/HomeTheaterFacade.java | 85 +++++++++++++++++++ Design Patterns/src/facade/Main.java | 21 +++++ Design Patterns/src/facade/PopcornPopper.java | 26 ++++++ Design Patterns/src/facade/Projector.java | 32 +++++++ Design Patterns/src/facade/Screen.java | 22 +++++ Design Patterns/src/facade/TheaterLights.java | 26 ++++++ Design Patterns/src/facade/Tuner.java | 37 ++++++++ 10 files changed, 416 insertions(+) create mode 100644 Design Patterns/src/facade/Amplifier.java create mode 100644 Design Patterns/src/facade/CDPlayer.java create mode 100644 Design Patterns/src/facade/DVDPlayer.java create mode 100644 Design Patterns/src/facade/HomeTheaterFacade.java create mode 100644 Design Patterns/src/facade/Main.java create mode 100644 Design Patterns/src/facade/PopcornPopper.java create mode 100644 Design Patterns/src/facade/Projector.java create mode 100644 Design Patterns/src/facade/Screen.java create mode 100644 Design Patterns/src/facade/TheaterLights.java create mode 100644 Design Patterns/src/facade/Tuner.java diff --git a/Design Patterns/src/facade/Amplifier.java b/Design Patterns/src/facade/Amplifier.java new file mode 100644 index 0000000..2166354 --- /dev/null +++ b/Design Patterns/src/facade/Amplifier.java @@ -0,0 +1,46 @@ +package facade; + +public class Amplifier { + + private final String description; + private Tuner tuner; + private DVDPlayer dvd; + private CDPlayer cd; + private int volume; + + public Amplifier(String description) { + this.description = description; + } + + public void on() { + System.out.println(description + " is On"); + } + + public void off() { + System.out.println(description + " is Off"); + } + + public void setSurroundSound() { + System.out.println(description + " Surround Sound enabled"); + } + + public void setStereoSound() { + System.out.println(description + " Stereo Mode enabled"); + } + + public void setTuner(Tuner tuner) { + this.tuner = tuner; + } + + public void setDvd(DVDPlayer dvd) { + this.dvd = dvd; + } + + public void setCd(CDPlayer cd) { + this.cd = cd; + } + + public void setVolume(int volume) { + this.volume = volume; + } +} diff --git a/Design Patterns/src/facade/CDPlayer.java b/Design Patterns/src/facade/CDPlayer.java new file mode 100644 index 0000000..df059eb --- /dev/null +++ b/Design Patterns/src/facade/CDPlayer.java @@ -0,0 +1,57 @@ +package facade; + +public class CDPlayer { + + private final String description; + private int currentTrack; + private final Amplifier amplifier; + private String title; + + public CDPlayer(String description, Amplifier amplifier) { + this.description = description; + this.amplifier = amplifier; + } + + public void on() { + System.out.println(description + " on"); + } + + public void off() { + System.out.println(description + " off"); + } + + public void eject() { + title = null; + System.out.println(description + " eject"); + } + + public void play(String title) { + this.title = title; + currentTrack = 0; + System.out.println(description + " playing \"" + title + "\""); + } + + public void play(int track) { + if (title == null) { + System.out.println(description + " can't play track " + currentTrack + + ", no cd inserted"); + } else { + currentTrack = track; + System.out.println(description + " playing track " + currentTrack); + } + } + + public void stop() { + currentTrack = 0; + System.out.println(description + " stopped"); + } + + public void pause() { + System.out.println(description + " paused \"" + title + "\""); + } + + @Override + public String toString() { + return description; + } +} \ No newline at end of file diff --git a/Design Patterns/src/facade/DVDPlayer.java b/Design Patterns/src/facade/DVDPlayer.java new file mode 100644 index 0000000..9ba52dc --- /dev/null +++ b/Design Patterns/src/facade/DVDPlayer.java @@ -0,0 +1,64 @@ +package facade; + +public class DVDPlayer { + + private final String description; + private int currentTrack; + private Amplifier amplifier; + private String movie; + + public DVDPlayer(String description, Amplifier amplifier) { + this.description = description; + this.amplifier = amplifier; + } + + public void on() { + System.out.println(description + " on"); + } + + public void off() { + System.out.println(description + " off"); + } + + public void eject() { + movie = null; + System.out.println(description + " eject"); + } + + public void play(String movie) { + this.movie = movie; + currentTrack = 0; + System.out.println(description + " playing \"" + movie + "\""); + } + + public void play(int track) { + if (movie == null) { + System.out.println(description + " can't play track " + track + " no dvd inserted"); + } else { + currentTrack = track; + System.out.println(description + " playing track " + currentTrack + " of \"" + movie + "\""); + } + } + + public void stop() { + currentTrack = 0; + System.out.println(description + " stopped \"" + movie + "\""); + } + + public void pause() { + System.out.println(description + " paused \"" + movie + "\""); + } + + public void setTwoChannelAudio() { + System.out.println(description + " set two channel audio"); + } + + public void setSurroundAudio() { + System.out.println(description + " set surround audio"); + } + + @Override + public String toString() { + return description; + } +} \ No newline at end of file diff --git a/Design Patterns/src/facade/HomeTheaterFacade.java b/Design Patterns/src/facade/HomeTheaterFacade.java new file mode 100644 index 0000000..85e9b4a --- /dev/null +++ b/Design Patterns/src/facade/HomeTheaterFacade.java @@ -0,0 +1,85 @@ +package facade; + +public class HomeTheaterFacade { + private final Amplifier amp; + private final Tuner tuner; + private final DVDPlayer dvd; + private final CDPlayer cd; + private final Projector projector; + private final TheaterLights lights; + private final Screen screen; + private final PopcornPopper popper; + + public HomeTheaterFacade(Amplifier amp, Tuner tuner, DVDPlayer dvd, CDPlayer cd, Projector projector, Screen screen, TheaterLights lights, PopcornPopper popper) { + this.amp = amp; + this.tuner = tuner; + this.dvd = dvd; + this.cd = cd; + this.projector = projector; + this.screen = screen; + this.lights = lights; + this.popper = popper; + } + + public void watchMovie(String movie) { + System.out.println("Get ready to watch a movie..."); + popper.on(); + popper.pop(); + lights.dim(10); + screen.down(); + projector.on(); + projector.wideScreenMode(); + amp.on(); + amp.setDvd(dvd); + amp.setSurroundSound(); + amp.setVolume(5); + dvd.on(); + dvd.play(movie); + } + + public void endMovie() { + System.out.println("Shutting movie theater down..."); + popper.off(); + lights.on(); + screen.up(); + projector.off(); + amp.off(); + dvd.stop(); + dvd.eject(); + dvd.off(); + } + + public void listenToCd(String cdTitle) { + System.out.println("Get ready for an audiopile experence..."); + lights.on(); + amp.on(); + amp.setVolume(5); + amp.setCd(cd); + amp.setStereoSound(); + cd.on(); + cd.play(cdTitle); + } + + public void endCd() { + System.out.println("Shutting down CD..."); + amp.off(); + amp.setCd(cd); + cd.eject(); + cd.off(); + } + + public void listenToRadio(double frequency) { + System.out.println("Tuning in the airwaves..."); + tuner.on(); + tuner.setFrequency(frequency); + amp.on(); + amp.setVolume(5); + amp.setTuner(tuner); + } + + public void endRadio() { + System.out.println("Shutting down the tuner..."); + tuner.off(); + amp.off(); + } +} diff --git a/Design Patterns/src/facade/Main.java b/Design Patterns/src/facade/Main.java new file mode 100644 index 0000000..61d6767 --- /dev/null +++ b/Design Patterns/src/facade/Main.java @@ -0,0 +1,21 @@ +package facade; + +public class Main { + public static void main(String[] args) { + + Amplifier amp = new Amplifier("Top-O-Line Amplifier"); + Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner"); + DVDPlayer dvd = new DVDPlayer("Top-O-Line DVD Player", amp); + CDPlayer cd = new CDPlayer("Top-O-Line CD Player", amp); + Projector projector = new Projector("Top-O-Line Projector", dvd); + TheaterLights lights = new TheaterLights("Theater Ceiling Lights"); + Screen screen = new Screen("Theater Screen"); + PopcornPopper popper = new PopcornPopper("Popcorn Popper"); + + HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, tuner, dvd, cd, projector, screen, lights, popper); + + homeTheater.watchMovie("Raiders of the Lost Ark"); + System.out.println("\n"); + homeTheater.endMovie(); + } +} diff --git a/Design Patterns/src/facade/PopcornPopper.java b/Design Patterns/src/facade/PopcornPopper.java new file mode 100644 index 0000000..283b722 --- /dev/null +++ b/Design Patterns/src/facade/PopcornPopper.java @@ -0,0 +1,26 @@ +package facade; + +public class PopcornPopper { + private final String description; + + public PopcornPopper(String description) { + this.description = description; + } + + public void on() { + System.out.println(description + " On"); + } + + public void off() { + System.out.println(description + " Off"); + } + + public void pop() { + System.out.println(description + " popping popcorn!"); + } + + @Override + public String toString() { + return description; + } +} \ No newline at end of file diff --git a/Design Patterns/src/facade/Projector.java b/Design Patterns/src/facade/Projector.java new file mode 100644 index 0000000..23b0c0d --- /dev/null +++ b/Design Patterns/src/facade/Projector.java @@ -0,0 +1,32 @@ +package facade; + +public class Projector { + private final String description; + private final DVDPlayer dvdPlayer; + + public Projector(String description, DVDPlayer dvdPlayer) { + this.description = description; + this.dvdPlayer = dvdPlayer; + } + + public void on() { + System.out.println(description + " On"); + } + + public void off() { + System.out.println(description + " Off"); + } + + public void wideScreenMode() { + System.out.println(description + " in Widescreen Mode (16x9 Aspect Ratio)"); + } + + public void tvMode() { + System.out.println(description + " in TV Mode (4x3 Aspect Ratio)"); + } + + @Override + public String toString() { + return description; + } +} diff --git a/Design Patterns/src/facade/Screen.java b/Design Patterns/src/facade/Screen.java new file mode 100644 index 0000000..fdb8c5f --- /dev/null +++ b/Design Patterns/src/facade/Screen.java @@ -0,0 +1,22 @@ +package facade; + +public class Screen { + private final String description; + + public Screen(String description) { + this.description = description; + } + + public void up() { + System.out.println(description + " going up"); + } + + public void down() { + System.out.println(description + " going down"); + } + + @Override + public String toString() { + return description; + } +} diff --git a/Design Patterns/src/facade/TheaterLights.java b/Design Patterns/src/facade/TheaterLights.java new file mode 100644 index 0000000..2f77d17 --- /dev/null +++ b/Design Patterns/src/facade/TheaterLights.java @@ -0,0 +1,26 @@ +package facade; + +public class TheaterLights { + private final String description; + + public TheaterLights(String description) { + this.description = description; + } + + public void on() { + System.out.println(description + " on"); + } + + public void off() { + System.out.println(description + " off"); + } + + public void dim(int level) { + System.out.println(description + " dimming to " + level + "%"); + } + + @Override + public String toString() { + return description; + } +} diff --git a/Design Patterns/src/facade/Tuner.java b/Design Patterns/src/facade/Tuner.java new file mode 100644 index 0000000..0ee2aae --- /dev/null +++ b/Design Patterns/src/facade/Tuner.java @@ -0,0 +1,37 @@ +package facade; + +public class Tuner { + private final String description; + private Amplifier amplifier; + private double frequency; + + public Tuner(String description) { + this.description = description; + } + + public void on() { + System.out.println(description + " on"); + } + + public void off() { + System.out.println(description + " off"); + } + + public void setFrequency(double frequency) { + System.out.println(description + " setting frequency to " + frequency); + this.frequency = frequency; + } + + public void setAm() { + System.out.println(description + " setting AM mode"); + } + + public void setFm() { + System.out.println(description + " setting FM mode"); + } + + @Override + public String toString() { + return description; + } +} \ No newline at end of file