-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder_pattern.py
112 lines (78 loc) · 1.94 KB
/
builder_pattern.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class Packing:
def pack(self)->str:
pass
class Wrapper(Packing):
def pack(self)->str:
return 'Wrapper'
def __str__(self):
return 'Wrapper'
class Bottle(Packing):
def pack(self)->str:
return 'Bottle'
def __str__(self):
return 'Bottle'
class Item:
def name(self)->str:
pass
def packing(self)->Packing:
pass
def price(self) -> float:
pass
class Burger(Item):
def packing(self)->Packing:
return Wrapper()
def price(self)->float:
pass
class ColdDrink(Item):
def packing(self)->Packing:
return Bottle()
def price(self)->float:
pass
class VegBurger(Burger):
def price(self)->float:
return 25.0
def name(self)->str:
return "Veg Burger"
class ChickenBurger(Burger):
def price(self) -> float:
return 50.0
def name(self) -> str:
return "Chicken Burger"
class Coke(ColdDrink):
def price(self) -> float:
return 30.0
def name(self) -> str:
return "Coke"
class Pepsi(ColdDrink):
def price(self) -> float:
return 36.0
def name(self) -> str:
return "Pepsi"
class Meal:
items = list()
def addItem(self, item: 'Item'):
self.items.append(item)
def getCost(self)->float:
cost = 0.0
for x in self.items:
cost += x.price()
return cost
def showItems(self):
for x in self.items:
print(str(x.name()))
print(str(x.packing()))
print(str(x.price()))
class MealBuilder:
def prepareVegMeal(self):
meal = Meal()
meal.addItem(VegBurger())
meal.addItem(Coke())
return meal
def prepareNonVegMeal(self):
meal = Meal()
meal.addItem(ChickenBurger())
meal.addItem(Pepsi())
return meal
meal_builder = MealBuilder()
meal = meal_builder.prepareNonVegMeal()
meal.showItems()