diff --git a/README.md b/README.md index a8f18fb..5636fc4 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ 1. [Декоратор](decorator/) 2. [Адаптер](adapter/) +2. [Фасад](facade/) ### Пораждающие diff --git a/adapter/README.md b/adapter/README.md index 6447f72..755074f 100644 --- a/adapter/README.md +++ b/adapter/README.md @@ -12,8 +12,8 @@ ![scheme-3](scheme-3.png) -![scheme-3](scheme-4.png) +![scheme-4](scheme-4.png) -![scheme-3](scheme-5.png) +![scheme-5](scheme-5.png) -![scheme-3](scheme-6.png) +![scheme-6](scheme-6.png) diff --git a/facade/README.md b/facade/README.md new file mode 100644 index 0000000..1d5e918 --- /dev/null +++ b/facade/README.md @@ -0,0 +1,17 @@ +# Фасад + +Структурный паттерн проектирования, который предоставляет простой интерфейс к сложной системе классов, библиотеке или фреймворку. + +Паттерн **Фасад** предоставляет унифицированный интерфейс к группе интерфейсов подсистемы. Фасад определяет высокоуровневый интерфейс, упрощающий работу с подсистемой. + +# Принципы + +- Принцип минимальной информированности: общайтесь только с близкими друзьями. + +## Схемы + +![scheme-1](scheme-1.png) + +![scheme-2](scheme-2.png) + +![scheme-3](scheme-3.png) diff --git a/facade/hometheater/amplifier.d b/facade/hometheater/amplifier.d new file mode 100644 index 0000000..12d91c6 --- /dev/null +++ b/facade/hometheater/amplifier.d @@ -0,0 +1,59 @@ +module amplifier; + +import tuner, streamingplayer; +import std.stdio : writeln; +import std.conv : to; + +class Amplifier +{ + private string description; + private Tuner tuner; + private StreamingPlayer player; + + this(string description) + { + this.description = description; + } + + void on() + { + writeln(description ~ " on"); + } + + void off() + { + writeln(description ~ " off"); + } + + void setStereoSound() + { + writeln(description ~ " stereo mode on"); + } + + void setSurroundSound() + { + writeln(description ~ " surround sound on (5 speakers, 1 subwoofer)"); + } + + void setVolume(int level) + { + writeln(description ~ " setting volume to " ~ level.to!string); + } + + void setTuner(Tuner tuner) + { + writeln(description ~ " setting tuner to " ~ tuner.to!string); + this.tuner = tuner; + } + + void setStreamingPlayer(StreamingPlayer player) + { + writeln(description ~ " setting Streaming player to " ~ player.to!string); + this.player = player; + } + + override string toString() const + { + return description; + } +} diff --git a/facade/hometheater/app.d b/facade/hometheater/app.d new file mode 100644 index 0000000..46a25c2 --- /dev/null +++ b/facade/hometheater/app.d @@ -0,0 +1,21 @@ +import amplifier, tuner, streamingplayer, cdplayer, projector, theaterlights, screen, popcornpopper, hometheaterfacade; + +void main() +{ + auto amp = new Amplifier("Amplifier"); + auto tuner = new Tuner("AM/FM Tuner", amp); + auto player = new StreamingPlayer("Streaming Player", amp); + auto cd = new CdPlayer("CD Player", amp); + auto projector = new Projector("Projector", player); + auto lights = new TheaterLights("Theater Ceiling Lights"); + auto screen = new Screen("Theater Screen"); + auto popper = new PopcornPopper("Popcorn Popper"); + + auto homeTheater = new HomeTheaterFacade(amp, tuner, player, cd, projector, lights, screen, popper); + + homeTheater.watchMovie("Raiders of the Lost Ark"); + homeTheater.endMovie(); + + homeTheater.listenToRadio(101.5); + homeTheater.endRadio(); +} diff --git a/facade/hometheater/cdplayer.d b/facade/hometheater/cdplayer.d new file mode 100644 index 0000000..171a2fb --- /dev/null +++ b/facade/hometheater/cdplayer.d @@ -0,0 +1,71 @@ +module cdplayer; + +import amplifier; +import std.stdio : writeln; +import std.conv : to; + +class CdPlayer +{ + private string description; + private int currentTrack; + private Amplifier amplifier; + private string title; + + this(string description, Amplifier amplifier) + { + this.description = description; + this.amplifier = amplifier; + } + + void on() + { + writeln(description ~ " on"); + } + + void off() + { + writeln(description ~ " off"); + } + + void eject() + { + title = null; + writeln(description ~ " eject"); + } + + void play(string title) + { + this.title = title; + currentTrack = 0; + writeln(description ~ " playing \"" ~ title ~ "\""); + } + + void play(int track) + { + if (title is null) + { + writeln(description ~ " can't play track " ~ currentTrack.to!string ~ " no cd inserted"); + } + else + { + currentTrack = track; + writeln(description ~ " playing track " ~ currentTrack.to!string); + } + } + + void stop() + { + currentTrack = 0; + writeln(description ~ " stopped"); + } + + void pause() + { + writeln(description ~ " paused \"" ~ title ~ "\""); + } + + override string toString() const + { + return description; + } +} diff --git a/facade/hometheater/hometheaterfacade.d b/facade/hometheater/hometheaterfacade.d new file mode 100644 index 0000000..2307180 --- /dev/null +++ b/facade/hometheater/hometheaterfacade.d @@ -0,0 +1,82 @@ +module hometheaterfacade; + +import std.stdio : writeln; +import amplifier, tuner, streamingplayer, cdplayer, projector, theaterlights, screen, popcornpopper; + +class HomeTheaterFacade +{ + private Amplifier amp; + private Tuner tuner; + private StreamingPlayer player; + private CdPlayer cd; + private Projector projector; + private TheaterLights lights; + private Screen screen; + private PopcornPopper popper; + + this(Amplifier amp, + Tuner tuner, + StreamingPlayer player, + CdPlayer cd, + Projector projector, + TheaterLights lights, + Screen screen, + PopcornPopper popper) + { + this.amp = amp; + this.tuner = tuner; + this.player = player; + this.cd = cd; + this.projector = projector; + this.lights = lights; + this.screen = screen; + this.popper = popper; + } + + void watchMovie(string movie) + { + writeln("Get ready to watch a movie..."); + popper.on(); + popper.pop(); + lights.dim(10); + screen.down(); + projector.on(); + projector.wideScreenMode(); + amp.on(); + amp.setStreamingPlayer(player); + amp.setSurroundSound(); + amp.setVolume(5); + player.on(); + player.play(movie); + } + + + void endMovie() + { + writeln("Shutting movie theater down..."); + popper.off(); + lights.on(); + screen.up(); + projector.off(); + amp.off(); + player.stop(); + player.off(); + } + + void listenToRadio(double frequency) + { + writeln("Tuning in the airwaves..."); + tuner.on(); + tuner.setFrequency(frequency); + amp.on(); + amp.setVolume(5); + amp.setTuner(tuner); + } + + void endRadio() + { + writeln("Shutting down the tuner..."); + tuner.off(); + amp.off(); + } +} diff --git a/facade/hometheater/popcornpopper.d b/facade/hometheater/popcornpopper.d new file mode 100644 index 0000000..f417142 --- /dev/null +++ b/facade/hometheater/popcornpopper.d @@ -0,0 +1,34 @@ +module popcornpopper; + +import std.stdio : writeln; + + +class PopcornPopper +{ + private string description; + + this(string description) + { + this.description = description; + } + + void on() + { + writeln(description ~ " on"); + } + + void off() + { + writeln(description ~ " off"); + } + + void pop() + { + writeln(description ~ " popping popcorn!"); + } + + override string toString() const + { + return description; + } +} diff --git a/facade/hometheater/projector.d b/facade/hometheater/projector.d new file mode 100644 index 0000000..a8885fb --- /dev/null +++ b/facade/hometheater/projector.d @@ -0,0 +1,41 @@ +module projector; + +import streamingplayer; +import std.stdio : writeln; + +class Projector +{ + private string description; + private StreamingPlayer player; + + this(string description, StreamingPlayer player) + { + this.description = description; + this.player = player; + } + + void on() + { + writeln(description ~ " on"); + } + + void off() + { + writeln(description ~ " off"); + } + + void wideScreenMode() + { + writeln(description ~ " in widescreen mode (16x9 aspect ratio)"); + } + + void tvMode() + { + writeln(description ~ " in tv mode (4x3 aspect ratio)"); + } + + override string toString() const + { + return description; + } +} diff --git a/facade/hometheater/screen.d b/facade/hometheater/screen.d new file mode 100644 index 0000000..1cc5ae3 --- /dev/null +++ b/facade/hometheater/screen.d @@ -0,0 +1,28 @@ +module screen; + +import std.stdio : writeln; + +class Screen +{ + private string description; + + this(string description) + { + this.description = description; + } + + void up() + { + writeln(description ~ " going up"); + } + + void down() + { + writeln(description ~ " going down"); + } + + override string toString() const + { + return description; + } +} diff --git a/facade/hometheater/streamingplayer.d b/facade/hometheater/streamingplayer.d new file mode 100644 index 0000000..2438332 --- /dev/null +++ b/facade/hometheater/streamingplayer.d @@ -0,0 +1,75 @@ +module streamingplayer; + +import amplifier; +import std.stdio : writeln; +import std.conv : to; + +class StreamingPlayer +{ + private string description; + private int currentChapter; + private Amplifier amplifier; + private string movie; + + this(string description, Amplifier amplifier) + { + this.description = description; + this.amplifier = amplifier; + } + + void on() + { + writeln(description ~ " on"); + } + + void off() + { + writeln(description ~ " off"); + } + + void play(string movie) + { + this.movie = movie; + currentChapter = 0; + writeln(description ~ " playing \"" ~ movie ~ "\""); + } + + void play(int chapter) + { + if (movie is null) + { + writeln(description ~ " can't play chapter " ~ chapter.to!string ~ " on movie selected"); + } + else + { + currentChapter = chapter; + writeln(description ~ " playing chapter " ~ currentChapter.to!string ~ " off \"" ~ movie ~ "\""); + } + } + + void stop() + { + currentChapter = 0; + writeln(description ~ " stopped \"" ~ movie ~ "\""); + } + + void pause() + { + writeln(description ~ " paused \"" ~ movie ~ "\""); + } + + void setTwoChannelAudio() + { + writeln(description ~ " set two channel audio"); + } + + void setSurroundAudio() + { + writeln(description ~ " set surround audio"); + } + + override string toString() const + { + return description; + } +} diff --git a/facade/hometheater/theaterlights.d b/facade/hometheater/theaterlights.d new file mode 100644 index 0000000..42da269 --- /dev/null +++ b/facade/hometheater/theaterlights.d @@ -0,0 +1,34 @@ +module theaterlights; + +import std.stdio : writeln; +import std.conv : to; + +class TheaterLights +{ + private string description; + + this(string description) + { + this.description = description; + } + + void on() + { + writeln(description ~ " on"); + } + + void off() + { + writeln(description ~ " off"); + } + + void dim(int level) + { + writeln(description ~ " dimming to " ~ level.to!string ~ "%"); + } + + override string toString() const + { + return description; + } +} diff --git a/facade/hometheater/tuner.d b/facade/hometheater/tuner.d new file mode 100644 index 0000000..732864a --- /dev/null +++ b/facade/hometheater/tuner.d @@ -0,0 +1,49 @@ +module tuner; + +import amplifier; +import std.stdio : writeln; +import std.conv : to; + +class Tuner +{ + private string description; + private Amplifier amplifier; + private double frequency; + + this(string description, Amplifier amplifier) + { + this.description = description; + this.amplifier = amplifier; + } + + void on() + { + writeln(description ~ " on"); + } + + void off() + { + writeln(description ~ " off"); + } + + void setFrequency(double frequency) + { + writeln(description ~ " setting frequency to " ~ frequency.to!string); + this.frequency = frequency; + } + + void setAm() + { + writeln(description ~ " setting AM mode"); + } + + void setFm() + { + writeln(description ~ " setting FM mode"); + } + + override string toString() const + { + return description; + } +} diff --git a/facade/scheme-1.png b/facade/scheme-1.png new file mode 100644 index 0000000..b02d83b Binary files /dev/null and b/facade/scheme-1.png differ diff --git a/facade/scheme-2.png b/facade/scheme-2.png new file mode 100644 index 0000000..2d1958a Binary files /dev/null and b/facade/scheme-2.png differ diff --git a/facade/scheme-3.png b/facade/scheme-3.png new file mode 100644 index 0000000..73192b6 Binary files /dev/null and b/facade/scheme-3.png differ