Merge branch 'D' of zhirov.kz:alexander/patterns into D
|
@ -14,6 +14,8 @@
|
|||
### Структурные
|
||||
|
||||
1. [Декоратор](decorator/)
|
||||
2. [Адаптер](adapter/)
|
||||
2. [Фасад](facade/)
|
||||
|
||||
### Пораждающие
|
||||
|
||||
|
|
|
@ -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)
|
After Width: | Height: | Size: 197 KiB |
After Width: | Height: | Size: 104 KiB |
After Width: | Height: | Size: 61 KiB |
After Width: | Height: | Size: 52 KiB |
After Width: | Height: | Size: 161 KiB |
After Width: | Height: | Size: 205 KiB |
|
@ -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();
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
module lib.duck;
|
||||
|
||||
interface Duck
|
||||
{
|
||||
void quack();
|
||||
void fly();
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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;
|
|
@ -0,0 +1,7 @@
|
|||
module lib.turkey;
|
||||
|
||||
interface Turkey
|
||||
{
|
||||
void gobble();
|
||||
void fly();
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
# Фасад
|
||||
|
||||
Структурный паттерн проектирования, который предоставляет простой интерфейс к сложной системе классов, библиотеке или фреймворку.
|
||||
|
||||
Паттерн **Фасад** предоставляет унифицированный интерфейс к группе интерфейсов подсистемы. Фасад определяет высокоуровневый интерфейс, упрощающий работу с подсистемой.
|
||||
|
||||
# Принципы
|
||||
|
||||
- Принцип минимальной информированности: общайтесь только с близкими друзьями.
|
||||
|
||||
## Схемы
|
||||
|
||||
![scheme-1](scheme-1.png)
|
||||
|
||||
![scheme-2](scheme-2.png)
|
||||
|
||||
![scheme-3](scheme-3.png)
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
After Width: | Height: | Size: 132 KiB |
After Width: | Height: | Size: 103 KiB |
After Width: | Height: | Size: 310 KiB |