From 0c320c05a40ab62e5d25129a12525ad699216f89 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 17 Nov 2022 02:25:52 +0300 Subject: [PATCH] template method --- README.md | 1 + templatemethod/README.md | 11 +++++ templatemethod/barista/app.d | 21 +++++++++ templatemethod/barista/caffeinebeverage.d | 28 ++++++++++++ .../barista/caffeinebeveragewithhook.d | 36 +++++++++++++++ templatemethod/barista/coffee.d | 17 +++++++ templatemethod/barista/coffeewithhook.d | 45 +++++++++++++++++++ templatemethod/barista/tea.d | 17 +++++++ templatemethod/barista/teawithhook.d | 45 +++++++++++++++++++ templatemethod/simplebarista/app.d | 12 +++++ templatemethod/simplebarista/coffee.d | 34 ++++++++++++++ templatemethod/simplebarista/tea.d | 34 ++++++++++++++ 12 files changed, 301 insertions(+) create mode 100644 templatemethod/README.md create mode 100644 templatemethod/barista/app.d create mode 100644 templatemethod/barista/caffeinebeverage.d create mode 100644 templatemethod/barista/caffeinebeveragewithhook.d create mode 100644 templatemethod/barista/coffee.d create mode 100644 templatemethod/barista/coffeewithhook.d create mode 100644 templatemethod/barista/tea.d create mode 100644 templatemethod/barista/teawithhook.d create mode 100644 templatemethod/simplebarista/app.d create mode 100644 templatemethod/simplebarista/coffee.d create mode 100644 templatemethod/simplebarista/tea.d diff --git a/README.md b/README.md index cfd55dc..f5e751e 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ 1. [Стратегия](strategy/) 2. [Наблюдатель](observer/) 3. [Команда](command/) +4. [Шаблонный метод](templatemethod/) ### Структурные diff --git a/templatemethod/README.md b/templatemethod/README.md new file mode 100644 index 0000000..e401bfc --- /dev/null +++ b/templatemethod/README.md @@ -0,0 +1,11 @@ +# Шаблонный метод + +Поведенческий паттерн проектирования, который определяет скелет алгоритма, перекладывая ответственность за некоторые его шаги на подклассы. Паттерн позволяет подклассам переопределять шаги алгоритма, не меняя его общей структуры. + +Паттерн **Шаблонный Метод** задает «скелет» алгоритма в методе, оставляя определение реализации некоторых шагов субклассам. Субклассы могут переопределять некоторые части алгоритма без изменения его структуры. + +## Принципы + +- Не вызывайте нас - мы вас сами вызовем + +Алгоритм определяется суперклассом, поэтому последний должен сам обращаться к субклассам, когда потребуется. diff --git a/templatemethod/barista/app.d b/templatemethod/barista/app.d new file mode 100644 index 0000000..860d360 --- /dev/null +++ b/templatemethod/barista/app.d @@ -0,0 +1,21 @@ +import std.stdio : writeln; +import coffee, tea, coffeewithhook, teawithhook; + +void main() +{ + auto tea = new Tea(); + auto coffee = new Coffee(); + + writeln("\nMaking tea..."); + tea.prepareRecipe(); + writeln("\nMaking coffee..."); + coffee.prepareRecipe(); + + auto teaHook = new TeaWithHook(); + auto coffeeHook = new CoffeeWithHook(); + + writeln("\nMaking tea..."); + teaHook.prepareRecipe(); + writeln("\nMaking coffee..."); + coffeeHook.prepareRecipe(); +} diff --git a/templatemethod/barista/caffeinebeverage.d b/templatemethod/barista/caffeinebeverage.d new file mode 100644 index 0000000..a464abf --- /dev/null +++ b/templatemethod/barista/caffeinebeverage.d @@ -0,0 +1,28 @@ +module caffeinebeverage; + +import std.stdio : writeln; + +abstract class CaffeineBeverage +{ + final void prepareRecipe() + { + boilWater(); + brew(); + pourInCup(); + addCondiments(); + } + + void brew(); + + void addCondiments(); + + void boilWater() + { + writeln("Boiling water"); + } + + void pourInCup() + { + writeln("Pouring into cup"); + } +} diff --git a/templatemethod/barista/caffeinebeveragewithhook.d b/templatemethod/barista/caffeinebeveragewithhook.d new file mode 100644 index 0000000..1541570 --- /dev/null +++ b/templatemethod/barista/caffeinebeveragewithhook.d @@ -0,0 +1,36 @@ +module caffeinebeveragewithhook; + +import std.stdio : writeln; + +abstract class CaffeineBeverageWithHook +{ + final void prepareRecipe() + { + boilWater(); + brew(); + pourInCup(); + if (customerWantsCondiments()) + { + addCondiments(); + } + } + + void brew(); + + void addCondiments(); + + void boilWater() + { + writeln("Boiling water"); + } + + void pourInCup() + { + writeln("Pouring into cup"); + } + + bool customerWantsCondiments() + { + return true; + } +} diff --git a/templatemethod/barista/coffee.d b/templatemethod/barista/coffee.d new file mode 100644 index 0000000..d44d520 --- /dev/null +++ b/templatemethod/barista/coffee.d @@ -0,0 +1,17 @@ +module coffee; + +import std.stdio : writeln; +import caffeinebeverage; + +class Coffee : CaffeineBeverage +{ + override void brew() + { + writeln("Dripping Coffee through filter"); + } + + override void addCondiments() + { + writeln("Adding Sugar and Milk"); + } +} diff --git a/templatemethod/barista/coffeewithhook.d b/templatemethod/barista/coffeewithhook.d new file mode 100644 index 0000000..330f260 --- /dev/null +++ b/templatemethod/barista/coffeewithhook.d @@ -0,0 +1,45 @@ +module coffeewithhook; + +import std.stdio : write, writeln, readln; +import std.uni : toLower; +import std.algorithm : startsWith; +import caffeinebeveragewithhook; + +class CoffeeWithHook : CaffeineBeverageWithHook +{ + override void brew() + { + writeln("Dripping Coffee through filter"); + } + + override void addCondiments() + { + writeln("Adding Sugar and Milk"); + } + + override bool customerWantsCondiments() + { + if (getUserInput().toLower().startsWith("y")) + { + return true; + } + else + { + return false; + } + } + + string getUserInput() + { + string answer; + + write("Would you like milk and sugar with your coffee (y/n)? "); + + if ((answer = readln()) !is null) + { + return answer; + } + + return "no"; + } +} diff --git a/templatemethod/barista/tea.d b/templatemethod/barista/tea.d new file mode 100644 index 0000000..13fdc10 --- /dev/null +++ b/templatemethod/barista/tea.d @@ -0,0 +1,17 @@ +module tea; + +import std.stdio : writeln; +import caffeinebeverage; + +class Tea : CaffeineBeverage +{ + override void brew() + { + writeln("Steeping the tea"); + } + + override void addCondiments() + { + writeln("Adding Lemon"); + } +} diff --git a/templatemethod/barista/teawithhook.d b/templatemethod/barista/teawithhook.d new file mode 100644 index 0000000..f521994 --- /dev/null +++ b/templatemethod/barista/teawithhook.d @@ -0,0 +1,45 @@ +module teawithhook; + +import std.stdio : write, writeln, readln; +import std.uni : toLower; +import std.algorithm : startsWith; +import caffeinebeveragewithhook; + +class TeaWithHook : CaffeineBeverageWithHook +{ + override void brew() + { + writeln("Steeping the tea"); + } + + override void addCondiments() + { + writeln("Adding Lemon"); + } + + override bool customerWantsCondiments() + { + if (getUserInput().toLower().startsWith("y")) + { + return true; + } + else + { + return false; + } + } + + string getUserInput() + { + string answer; + + write("Would you like lemon with your tea (y/n)? "); + + if ((answer = readln()) !is null) + { + return answer; + } + + return "no"; + } +} diff --git a/templatemethod/simplebarista/app.d b/templatemethod/simplebarista/app.d new file mode 100644 index 0000000..95c51f0 --- /dev/null +++ b/templatemethod/simplebarista/app.d @@ -0,0 +1,12 @@ +import std.stdio : writeln; +import coffee, tea; + +void main() +{ + auto tea = new Tea(); + auto coffee = new Coffee(); + writeln("Making tea..."); + tea.prepareRecipe(); + writeln("Making coffee..."); + coffee.prepareRecipe(); +} diff --git a/templatemethod/simplebarista/coffee.d b/templatemethod/simplebarista/coffee.d new file mode 100644 index 0000000..7b9fc26 --- /dev/null +++ b/templatemethod/simplebarista/coffee.d @@ -0,0 +1,34 @@ +module coffee; + +import std.stdio : writeln; + +class Coffee +{ + void prepareRecipe() + { + boilWater(); + brewCoffeeGrinds(); + pourInCup(); + addSugarAndMilk(); + } + + void boilWater() + { + writeln("Boiling water"); + } + + void brewCoffeeGrinds() + { + writeln("Dripping Coffee through filter"); + } + + void pourInCup() + { + writeln("Pouring into cup"); + } + + void addSugarAndMilk() + { + writeln("Adding Sugar and Milk"); + } +} diff --git a/templatemethod/simplebarista/tea.d b/templatemethod/simplebarista/tea.d new file mode 100644 index 0000000..ca55a68 --- /dev/null +++ b/templatemethod/simplebarista/tea.d @@ -0,0 +1,34 @@ +module tea; + +import std.stdio : writeln; + +class Tea +{ + void prepareRecipe() + { + boilWater(); + steepTeaBag(); + pourInCup(); + addLemon(); + } + + void boilWater() + { + writeln("Boiling water"); + } + + void steepTeaBag() + { + writeln("Steeping the tea"); + } + + void addLemon() + { + writeln("Adding Lemon"); + } + + void pourInCup() + { + writeln("Pouring into cup"); + } +}