-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator_pattern.py
More file actions
73 lines (52 loc) · 1.71 KB
/
decorator_pattern.py
File metadata and controls
73 lines (52 loc) · 1.71 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
from abc import *
class Coffee(ABC):
@abstractmethod
def drink(self) -> None: ...
@abstractmethod
def get_cost(self) -> int: ...
class VanillaLatte(Coffee):
def drink(self) -> None:
print("Vanilla Latte drunk")
def get_cost(self) -> int:
return 4000S
class CoffeeDecorator(Coffee):
def __init__(self, coffee: Coffee):
self._coffee = coffee
def drink(self) -> None:
self._coffee.drink()
def get_cost(self) -> int:
return self._coffee.get_cost()
class Sugar(CoffeeDecorator):
def drink(self) -> None:
print("Sugar added")
super().drink()
def get_cost(self) -> int:
return super().get_cost() + 500
class Milk(CoffeeDecorator):
def drink(self) -> None:
print("Milk added")
super().drink()
def get_cost(self) -> int:
return super().get_cost() + 800
class WhippedCream(CoffeeDecorator):
def drink(self) -> None:
print("Whipped cream added")
super().drink()
def get_cost(self) -> int:
return super().get_cost() + 1000
if __name__ == '__main__':
basic_coffee = VanillaLatte()
basic_coffee.drink()
print(f"Cost: {basic_coffee.get_cost():,}won")
print("")
coffee_with_sugar = Sugar(basic_coffee)
coffee_with_sugar.drink()
print(f"Cost: {coffee_with_sugar.get_cost():,}won")
print("")
coffee_with_milk = Milk(coffee_with_sugar)
coffee_with_milk.drink()
print(f"Cost: {coffee_with_milk.get_cost():,}won")
print("")
coffee_with_cream = WhippedCream(coffee_with_milk)
coffee_with_cream.drink()
print(f"Cost: {coffee_with_cream.get_cost():,}원")