From 90c1b88af99c738ffe147d6981863504e8d7c8a0 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Fri, 11 Nov 2022 15:51:42 +0300 Subject: [PATCH] decorator --- decorator/coffee/app.d | 35 +++++++++++++++++ decorator/coffee/beverage.d | 14 +++++++ decorator/coffee/condimentdecorator.d | 8 ++++ decorator/coffee/darkroast.d | 16 ++++++++ decorator/coffee/decaf.d | 16 ++++++++ decorator/coffee/espresso.d | 16 ++++++++ decorator/coffee/houseblend.d | 16 ++++++++ decorator/coffee/milk.d | 25 ++++++++++++ decorator/coffee/mocha.d | 25 ++++++++++++ decorator/coffee/soy.d | 25 ++++++++++++ decorator/coffee/whip.d | 25 ++++++++++++ decorator/coffeewithsize/app.d | 38 +++++++++++++++++++ decorator/coffeewithsize/beverage.d | 27 +++++++++++++ decorator/coffeewithsize/condimentdecorator.d | 8 ++++ decorator/coffeewithsize/darkroast.d | 16 ++++++++ decorator/coffeewithsize/decaf.d | 16 ++++++++ decorator/coffeewithsize/espresso.d | 16 ++++++++ decorator/coffeewithsize/houseblend.d | 16 ++++++++ decorator/coffeewithsize/milk.d | 25 ++++++++++++ decorator/coffeewithsize/mocha.d | 25 ++++++++++++ decorator/coffeewithsize/soy.d | 38 +++++++++++++++++++ decorator/coffeewithsize/whip.d | 25 ++++++++++++ 22 files changed, 471 insertions(+) create mode 100644 decorator/coffee/app.d create mode 100644 decorator/coffee/beverage.d create mode 100644 decorator/coffee/condimentdecorator.d create mode 100644 decorator/coffee/darkroast.d create mode 100644 decorator/coffee/decaf.d create mode 100644 decorator/coffee/espresso.d create mode 100644 decorator/coffee/houseblend.d create mode 100644 decorator/coffee/milk.d create mode 100644 decorator/coffee/mocha.d create mode 100644 decorator/coffee/soy.d create mode 100644 decorator/coffee/whip.d create mode 100644 decorator/coffeewithsize/app.d create mode 100644 decorator/coffeewithsize/beverage.d create mode 100644 decorator/coffeewithsize/condimentdecorator.d create mode 100644 decorator/coffeewithsize/darkroast.d create mode 100644 decorator/coffeewithsize/decaf.d create mode 100644 decorator/coffeewithsize/espresso.d create mode 100644 decorator/coffeewithsize/houseblend.d create mode 100644 decorator/coffeewithsize/milk.d create mode 100644 decorator/coffeewithsize/mocha.d create mode 100644 decorator/coffeewithsize/soy.d create mode 100644 decorator/coffeewithsize/whip.d diff --git a/decorator/coffee/app.d b/decorator/coffee/app.d new file mode 100644 index 0000000..5f54df3 --- /dev/null +++ b/decorator/coffee/app.d @@ -0,0 +1,35 @@ +module decorator.coffee.app; + +import decorator.coffee.beverage; +import decorator.coffee.espresso; +import decorator.coffee.darkroast; +import decorator.coffee.houseblend; +import decorator.coffee.decaf; +import decorator.coffee.mocha; +import decorator.coffee.milk; +import decorator.coffee.soy; +import decorator.coffee.whip; +import std.stdio : writeln; + +void print(Beverage beverage) +{ + writeln(beverage.getDescription(), " $", beverage.cost()); +} + +void main() +{ + Beverage beverage = new Espresso(); + print(beverage); + + Beverage beverage2 = new DarkRoast(); + beverage2 = new Mocha(beverage2); + beverage2 = new Mocha(beverage2); + beverage2 = new Whip(beverage2); + print(beverage2); + + Beverage beverage3 = new HouseBlend(); + beverage3 = new Soy(beverage3); + beverage3 = new Mocha(beverage3); + beverage3 = new Whip(beverage3); + print(beverage3); +} \ No newline at end of file diff --git a/decorator/coffee/beverage.d b/decorator/coffee/beverage.d new file mode 100644 index 0000000..eb9fd29 --- /dev/null +++ b/decorator/coffee/beverage.d @@ -0,0 +1,14 @@ +module decorator.coffee.beverage; + +abstract class Beverage +{ +protected: + string description = "Unknown Beverage"; +public: + string getDescription() + { + return description; + } + + double cost(); +} diff --git a/decorator/coffee/condimentdecorator.d b/decorator/coffee/condimentdecorator.d new file mode 100644 index 0000000..a379f2c --- /dev/null +++ b/decorator/coffee/condimentdecorator.d @@ -0,0 +1,8 @@ +module decorator.coffee.condimentdecorator; + +import decorator.coffee.beverage; + +abstract class CondimerDecorator : Beverage +{ + override string getDescription(); +} diff --git a/decorator/coffee/darkroast.d b/decorator/coffee/darkroast.d new file mode 100644 index 0000000..a4f4a8a --- /dev/null +++ b/decorator/coffee/darkroast.d @@ -0,0 +1,16 @@ +module decorator.coffee.darkroast; + +import decorator.coffee.beverage; + +class DarkRoast : Beverage +{ + this() + { + description = "Dark Roast Coffee"; + } + + override double cost() + { + return .99; + } +} diff --git a/decorator/coffee/decaf.d b/decorator/coffee/decaf.d new file mode 100644 index 0000000..4d73166 --- /dev/null +++ b/decorator/coffee/decaf.d @@ -0,0 +1,16 @@ +module decorator.coffee.decaf; + +import decorator.coffee.beverage; + +class Decaf : Beverage +{ + this() + { + description = "Decaf Coffee"; + } + + override double cost() + { + return 1.05; + } +} diff --git a/decorator/coffee/espresso.d b/decorator/coffee/espresso.d new file mode 100644 index 0000000..4ce1198 --- /dev/null +++ b/decorator/coffee/espresso.d @@ -0,0 +1,16 @@ +module decorator.coffee.espresso; + +import decorator.coffee.beverage; + +class Espresso : Beverage +{ + this() + { + description = "Espresso"; + } + + override double cost() + { + return 1.99; + } +} diff --git a/decorator/coffee/houseblend.d b/decorator/coffee/houseblend.d new file mode 100644 index 0000000..c637ab8 --- /dev/null +++ b/decorator/coffee/houseblend.d @@ -0,0 +1,16 @@ +module decorator.coffee.houseblend; + +import decorator.coffee.beverage; + +class HouseBlend : Beverage +{ + this() + { + description = "House Blend Coffee"; + } + + override double cost() + { + return .89; + } +} diff --git a/decorator/coffee/milk.d b/decorator/coffee/milk.d new file mode 100644 index 0000000..faa3b5a --- /dev/null +++ b/decorator/coffee/milk.d @@ -0,0 +1,25 @@ +module decorator.coffee.milk; + +import decorator.coffee.beverage; +import decorator.coffee.condimentdecorator; + +class Milk : CondimerDecorator +{ +private: + Beverage beverage; +public: + this(Beverage beverage) + { + this.beverage = beverage; + } + + override string getDescription() + { + return beverage.getDescription() ~ ", Milk"; + } + + override double cost() + { + return .10 + beverage.cost(); + } +} diff --git a/decorator/coffee/mocha.d b/decorator/coffee/mocha.d new file mode 100644 index 0000000..35f5a76 --- /dev/null +++ b/decorator/coffee/mocha.d @@ -0,0 +1,25 @@ +module decorator.coffee.mocha; + +import decorator.coffee.beverage; +import decorator.coffee.condimentdecorator; + +class Mocha : CondimerDecorator +{ +private: + Beverage beverage; +public: + this(Beverage beverage) + { + this.beverage = beverage; + } + + override string getDescription() + { + return beverage.getDescription() ~ ", Mocha"; + } + + override double cost() + { + return .20 + beverage.cost(); + } +} diff --git a/decorator/coffee/soy.d b/decorator/coffee/soy.d new file mode 100644 index 0000000..5df6ac9 --- /dev/null +++ b/decorator/coffee/soy.d @@ -0,0 +1,25 @@ +module decorator.coffee.soy; + +import decorator.coffee.beverage; +import decorator.coffee.condimentdecorator; + +class Soy : CondimerDecorator +{ +private: + Beverage beverage; +public: + this(Beverage beverage) + { + this.beverage = beverage; + } + + override string getDescription() + { + return beverage.getDescription() ~ ", Soy"; + } + + override double cost() + { + return .15 + beverage.cost(); + } +} \ No newline at end of file diff --git a/decorator/coffee/whip.d b/decorator/coffee/whip.d new file mode 100644 index 0000000..b9c8516 --- /dev/null +++ b/decorator/coffee/whip.d @@ -0,0 +1,25 @@ +module decorator.coffee.whip; + +import decorator.coffee.beverage; +import decorator.coffee.condimentdecorator; + +class Whip : CondimerDecorator +{ +private: + Beverage beverage; +public: + this(Beverage beverage) + { + this.beverage = beverage; + } + + override string getDescription() + { + return beverage.getDescription() ~ ", Whip"; + } + + override double cost() + { + return .10 + beverage.cost(); + } +} diff --git a/decorator/coffeewithsize/app.d b/decorator/coffeewithsize/app.d new file mode 100644 index 0000000..eb1a462 --- /dev/null +++ b/decorator/coffeewithsize/app.d @@ -0,0 +1,38 @@ +module decorator.coffeewithsize.app; + +import decorator.coffeewithsize.beverage; +import decorator.coffeewithsize.espresso; +import decorator.coffeewithsize.darkroast; +import decorator.coffeewithsize.houseblend; +import decorator.coffeewithsize.decaf; +import decorator.coffeewithsize.mocha; +import decorator.coffeewithsize.milk; +import decorator.coffeewithsize.soy; +import decorator.coffeewithsize.whip; +import std.stdio : writeln; + +alias Size = Beverage.Size; + +void print(Beverage beverage) +{ + writeln(beverage.getDescription(), " $", beverage.cost()); +} + +void main() +{ + Beverage beverage = new Espresso(); + print(beverage); + + Beverage beverage2 = new DarkRoast(); + beverage2 = new Mocha(beverage2); + beverage2 = new Mocha(beverage2); + beverage2 = new Whip(beverage2); + print(beverage2); + + Beverage beverage3 = new HouseBlend(); + beverage3.setSize(Size.VENTI); + beverage3 = new Soy(beverage3); + beverage3 = new Mocha(beverage3); + beverage3 = new Whip(beverage3); + print(beverage3); +} diff --git a/decorator/coffeewithsize/beverage.d b/decorator/coffeewithsize/beverage.d new file mode 100644 index 0000000..fb2a757 --- /dev/null +++ b/decorator/coffeewithsize/beverage.d @@ -0,0 +1,27 @@ +module decorator.coffeewithsize.beverage; + +abstract class Beverage +{ +protected: + string description = "Unknown Beverage"; +public: + enum Size { TALL, GRANDE, VENTI } + Size size = Size.TALL; + + string getDescription() + { + return description; + } + + void setSize(Size size) + { + this.size = size; + } + + Size getSize() + { + return size; + } + + double cost(); +} diff --git a/decorator/coffeewithsize/condimentdecorator.d b/decorator/coffeewithsize/condimentdecorator.d new file mode 100644 index 0000000..5c95cb5 --- /dev/null +++ b/decorator/coffeewithsize/condimentdecorator.d @@ -0,0 +1,8 @@ +module decorator.coffeewithsize.condimentdecorator; + +import decorator.coffeewithsize.beverage; + +abstract class CondimerDecorator : Beverage +{ + override string getDescription(); +} diff --git a/decorator/coffeewithsize/darkroast.d b/decorator/coffeewithsize/darkroast.d new file mode 100644 index 0000000..b6b1904 --- /dev/null +++ b/decorator/coffeewithsize/darkroast.d @@ -0,0 +1,16 @@ +module decorator.coffeewithsize.darkroast; + +import decorator.coffeewithsize.beverage; + +class DarkRoast : Beverage +{ + this() + { + description = "Dark Roast Coffee"; + } + + override double cost() + { + return .99; + } +} diff --git a/decorator/coffeewithsize/decaf.d b/decorator/coffeewithsize/decaf.d new file mode 100644 index 0000000..5926a07 --- /dev/null +++ b/decorator/coffeewithsize/decaf.d @@ -0,0 +1,16 @@ +module decorator.coffeewithsize.decaf; + +import decorator.coffeewithsize.beverage; + +class Decaf : Beverage +{ + this() + { + description = "Decaf Coffee"; + } + + override double cost() + { + return 1.05; + } +} diff --git a/decorator/coffeewithsize/espresso.d b/decorator/coffeewithsize/espresso.d new file mode 100644 index 0000000..f46615b --- /dev/null +++ b/decorator/coffeewithsize/espresso.d @@ -0,0 +1,16 @@ +module decorator.coffeewithsize.espresso; + +import decorator.coffeewithsize.beverage; + +class Espresso : Beverage +{ + this() + { + description = "Espresso"; + } + + override double cost() + { + return 1.99; + } +} diff --git a/decorator/coffeewithsize/houseblend.d b/decorator/coffeewithsize/houseblend.d new file mode 100644 index 0000000..9304a34 --- /dev/null +++ b/decorator/coffeewithsize/houseblend.d @@ -0,0 +1,16 @@ +module decorator.coffeewithsize.houseblend; + +import decorator.coffeewithsize.beverage; + +class HouseBlend : Beverage +{ + this() + { + description = "House Blend Coffee"; + } + + override double cost() + { + return .89; + } +} diff --git a/decorator/coffeewithsize/milk.d b/decorator/coffeewithsize/milk.d new file mode 100644 index 0000000..03eb61a --- /dev/null +++ b/decorator/coffeewithsize/milk.d @@ -0,0 +1,25 @@ +module decorator.coffeewithsize.milk; + +import decorator.coffeewithsize.beverage; +import decorator.coffeewithsize.condimentdecorator; + +class Milk : CondimerDecorator +{ +private: + Beverage beverage; +public: + this(Beverage beverage) + { + this.beverage = beverage; + } + + override string getDescription() + { + return beverage.getDescription() ~ ", Milk"; + } + + override double cost() + { + return .10 + beverage.cost(); + } +} diff --git a/decorator/coffeewithsize/mocha.d b/decorator/coffeewithsize/mocha.d new file mode 100644 index 0000000..db2c703 --- /dev/null +++ b/decorator/coffeewithsize/mocha.d @@ -0,0 +1,25 @@ +module decorator.coffeewithsize.mocha; + +import decorator.coffeewithsize.beverage; +import decorator.coffeewithsize.condimentdecorator; + +class Mocha : CondimerDecorator +{ +private: + Beverage beverage; +public: + this(Beverage beverage) + { + this.beverage = beverage; + } + + override string getDescription() + { + return beverage.getDescription() ~ ", Mocha"; + } + + override double cost() + { + return .20 + beverage.cost(); + } +} diff --git a/decorator/coffeewithsize/soy.d b/decorator/coffeewithsize/soy.d new file mode 100644 index 0000000..b6c94c2 --- /dev/null +++ b/decorator/coffeewithsize/soy.d @@ -0,0 +1,38 @@ +module decorator.coffeewithsize.soy; + +import decorator.coffeewithsize.beverage; +import decorator.coffeewithsize.condimentdecorator; + +class Soy : CondimerDecorator +{ +private: + Beverage beverage; +public: + this(Beverage beverage) + { + this.beverage = beverage; + } + + override string getDescription() + { + return beverage.getDescription() ~ ", Soy"; + } + + override double cost() + { + double cost = beverage.cost(); + if (beverage.getSize() == Size.TALL) + { + cost += .10; + } + else if (beverage.getSize() == Size.GRANDE) + { + cost += .15; + } + else if (beverage.getSize() == Size.VENTI) + { + cost += .20; + } + return cost; + } +} \ No newline at end of file diff --git a/decorator/coffeewithsize/whip.d b/decorator/coffeewithsize/whip.d new file mode 100644 index 0000000..72a9791 --- /dev/null +++ b/decorator/coffeewithsize/whip.d @@ -0,0 +1,25 @@ +module decorator.coffeewithsize.whip; + +import decorator.coffeewithsize.beverage; +import decorator.coffeewithsize.condimentdecorator; + +class Whip : CondimerDecorator +{ +private: + Beverage beverage; +public: + this(Beverage beverage) + { + this.beverage = beverage; + } + + override string getDescription() + { + return beverage.getDescription() ~ ", Whip"; + } + + override double cost() + { + return .10 + beverage.cost(); + } +}