diff --git a/README.md b/README.md index f5e751e..4f743b3 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ ### Структурные 1. [Декоратор](decorator/) +2. [Адаптер](adapter/) +2. [Фасад](facade/) ### Пораждающие diff --git a/adapter/README.md b/adapter/README.md new file mode 100644 index 0000000..755074f --- /dev/null +++ b/adapter/README.md @@ -0,0 +1,19 @@ +# Адаптер + +Структурный паттерн проектирования, который позволяет объектам с несовместимыми интерфейсами работать вместе. + +Паттерн **Адаптер** преобразует интерфейс класса к другому интерфейсу, на который рассчитан клиент. Адаптер обеспечивает совместную работу классов, невозможную в обычных условиях из-за несовместимости интерфейсов. + +## Схемы + +![scheme-1](scheme-1.png) + +![scheme-2](scheme-2.png) + +![scheme-3](scheme-3.png) + +![scheme-4](scheme-4.png) + +![scheme-5](scheme-5.png) + +![scheme-6](scheme-6.png) diff --git a/adapter/scheme-1.png b/adapter/scheme-1.png new file mode 100644 index 0000000..263b71a Binary files /dev/null and b/adapter/scheme-1.png differ diff --git a/adapter/scheme-2.png b/adapter/scheme-2.png new file mode 100644 index 0000000..733f805 Binary files /dev/null and b/adapter/scheme-2.png differ diff --git a/adapter/scheme-3.png b/adapter/scheme-3.png new file mode 100644 index 0000000..45d1d1b Binary files /dev/null and b/adapter/scheme-3.png differ diff --git a/adapter/scheme-4.png b/adapter/scheme-4.png new file mode 100644 index 0000000..89989b8 Binary files /dev/null and b/adapter/scheme-4.png differ diff --git a/adapter/scheme-5.png b/adapter/scheme-5.png new file mode 100644 index 0000000..44cad3a Binary files /dev/null and b/adapter/scheme-5.png differ diff --git a/adapter/scheme-6.png b/adapter/scheme-6.png new file mode 100644 index 0000000..a0c09c4 Binary files /dev/null and b/adapter/scheme-6.png differ diff --git a/adapter/simpleadapter/app.d b/adapter/simpleadapter/app.d new file mode 100644 index 0000000..91309ee --- /dev/null +++ b/adapter/simpleadapter/app.d @@ -0,0 +1,28 @@ +module app; + +import lib; +import std.stdio : writeln; + +void main() +{ + Duck duck = new MallardDuck(); + + Turkey turkey = new WildTurkey(); + Duck turkeyAdapter = new TurkeyAdapter(turkey); + + writeln("The Turkey says..."); + turkey.gobble(); + turkey.fly(); + + writeln("\nThe Duck says..."); + testDuck(duck); + + writeln("\nThe TurkeyAdapter says..."); + testDuck(turkeyAdapter); +} + +void testDuck(Duck duck) +{ + duck.quack(); + duck.fly(); +} diff --git a/adapter/simpleadapter/lib/duck.d b/adapter/simpleadapter/lib/duck.d new file mode 100644 index 0000000..bb2ad78 --- /dev/null +++ b/adapter/simpleadapter/lib/duck.d @@ -0,0 +1,7 @@ +module lib.duck; + +interface Duck +{ + void quack(); + void fly(); +} diff --git a/adapter/simpleadapter/lib/mallarduck.d b/adapter/simpleadapter/lib/mallarduck.d new file mode 100644 index 0000000..5a7cbae --- /dev/null +++ b/adapter/simpleadapter/lib/mallarduck.d @@ -0,0 +1,17 @@ +module lib.mallarduck; + +import lib.duck; +import std.stdio : writeln; + +class MallardDuck : Duck +{ + void quack() + { + writeln("Quack!"); + } + + void fly() + { + writeln("I'm flying"); + } +} diff --git a/adapter/simpleadapter/lib/package.d b/adapter/simpleadapter/lib/package.d new file mode 100644 index 0000000..e5d953b --- /dev/null +++ b/adapter/simpleadapter/lib/package.d @@ -0,0 +1,7 @@ +module lib; + +public import lib.duck; +public import lib.mallarduck; +public import lib.turkey; +public import lib.turkeyadapter; +public import lib.wildturkey; diff --git a/adapter/simpleadapter/lib/turkey.d b/adapter/simpleadapter/lib/turkey.d new file mode 100644 index 0000000..46f6858 --- /dev/null +++ b/adapter/simpleadapter/lib/turkey.d @@ -0,0 +1,7 @@ +module lib.turkey; + +interface Turkey +{ + void gobble(); + void fly(); +} diff --git a/adapter/simpleadapter/lib/turkeyadapter.d b/adapter/simpleadapter/lib/turkeyadapter.d new file mode 100644 index 0000000..3c6726a --- /dev/null +++ b/adapter/simpleadapter/lib/turkeyadapter.d @@ -0,0 +1,27 @@ +module lib.turkeyadapter; + +import lib.turkey; +import lib.duck; + +class TurkeyAdapter : Duck +{ + Turkey turkey; + + this(Turkey turkey) + { + this.turkey = turkey; + } + + void quack() + { + turkey.gobble(); + } + + void fly() + { + foreach (val; 0..5) + { + turkey.fly(); + } + } +} diff --git a/adapter/simpleadapter/lib/wildturkey.d b/adapter/simpleadapter/lib/wildturkey.d new file mode 100644 index 0000000..da45f58 --- /dev/null +++ b/adapter/simpleadapter/lib/wildturkey.d @@ -0,0 +1,17 @@ +module lib.wildturkey; + +import lib.turkey; +import std.stdio : writeln; + +class WildTurkey : Turkey +{ + void gobble() + { + writeln("Gobble gobble"); + } + + void fly() + { + writeln("I'm flying a short distance"); + } +} 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