-
Notifications
You must be signed in to change notification settings - Fork 3
/
decorator.py
60 lines (40 loc) · 1.29 KB
/
decorator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from abc import abstractmethod
class Beverage:
def __init__(self, description="Unknown Beverage"):
self.description = description
@abstractmethod
def cost(self):
return 0.0
class Espresso(Beverage):
def __init__(self) -> None:
super().__init__("Espresso Coffee")
def cost(self):
return 0.5
class Capuccino(Beverage):
def __init__(self) -> None:
super().__init__("Capuccino Coffee")
def cost(self):
return 1.0
class CondimentDecorator(Beverage):
@abstractmethod
def cost(self):
return 0.0
class Milk(CondimentDecorator):
def __init__(self, beverage: Beverage):
self.beverage = beverage
self.description = self.beverage.description + " + Milk"
def cost(self):
return 0.1 + self.beverage.cost()
class Whip(CondimentDecorator):
def __init__(self, beverage: Beverage):
self.beverage = beverage
self.description = self.beverage.description + " + Whip"
def cost(self):
return 0.25 + self.beverage.cost()
beverage = Espresso()
print(f"{beverage.description}: {beverage.cost():.2f}")
beverage_2 = Capuccino()
beverage_2 = Milk(beverage_2)
beverage_2 = Milk(beverage_2)
beverage_2 = Whip(beverage_2)
print(f"{beverage_2.description}: {beverage_2.cost():.2f}")