decorator

This commit is contained in:
Alexander Zhirov 2022-11-11 15:51:42 +03:00
parent e815172862
commit 90c1b88af9
22 changed files with 471 additions and 0 deletions

35
decorator/coffee/app.d Normal file
View File

@ -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);
}

View File

@ -0,0 +1,14 @@
module decorator.coffee.beverage;
abstract class Beverage
{
protected:
string description = "Unknown Beverage";
public:
string getDescription()
{
return description;
}
double cost();
}

View File

@ -0,0 +1,8 @@
module decorator.coffee.condimentdecorator;
import decorator.coffee.beverage;
abstract class CondimerDecorator : Beverage
{
override string getDescription();
}

View File

@ -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;
}
}

16
decorator/coffee/decaf.d Normal file
View File

@ -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;
}
}

View File

@ -0,0 +1,16 @@
module decorator.coffee.espresso;
import decorator.coffee.beverage;
class Espresso : Beverage
{
this()
{
description = "Espresso";
}
override double cost()
{
return 1.99;
}
}

View File

@ -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;
}
}

25
decorator/coffee/milk.d Normal file
View File

@ -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();
}
}

25
decorator/coffee/mocha.d Normal file
View File

@ -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();
}
}

25
decorator/coffee/soy.d Normal file
View File

@ -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();
}
}

25
decorator/coffee/whip.d Normal file
View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -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();
}

View File

@ -0,0 +1,8 @@
module decorator.coffeewithsize.condimentdecorator;
import decorator.coffeewithsize.beverage;
abstract class CondimerDecorator : Beverage
{
override string getDescription();
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,16 @@
module decorator.coffeewithsize.espresso;
import decorator.coffeewithsize.beverage;
class Espresso : Beverage
{
this()
{
description = "Espresso";
}
override double cost()
{
return 1.99;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}