-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
141 lines (117 loc) · 2.11 KB
/
builder.go
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package pattern
import (
"fmt"
)
type Item interface {
Name() string
Price() float64
Packing() Packing
}
type Packing interface {
Pack() string
}
type Wrapper struct {
Packing
}
func (w *Wrapper) Pack() string {
return "wrapper"
}
type Bottle struct {
Packing
}
func (b *Bottle) Pack() string {
return "wrapper"
}
type Bugger struct {
Item
}
func (b *Bugger) Packing() Packing {
return new(Wrapper)
}
type VegBugger struct {
Bugger
}
func (v *VegBugger) Price() float64 {
return 25
}
func (v *VegBugger) Name() string {
return "vegBugger"
}
type ChickenBugger struct {
Bugger
}
func (c *ChickenBugger) Price() float64 {
return 50.5
}
func (c *ChickenBugger) Name() string {
return "ChickenBugger"
}
type ColdDrink struct {
Item
}
func (c *ColdDrink) Packing() Packing {
return new(Bottle)
}
type Coke struct {
ColdDrink
}
func (c *Coke) Price() float64 {
return 30
}
func (c *Coke) Name() string {
return "Coke"
}
type Pepsi struct {
ColdDrink
}
func (p *Pepsi) Price() float64 {
return 35
}
func (p *Pepsi) Name() string {
return "Pepsi"
}
type Meal struct {
Items []Item
}
func (m *Meal) AddItem(item Item) {
m.Items = append(m.Items, item)
}
func (m *Meal) GetCost() float64 {
var cost float64
for _, item := range m.Items {
cost += item.Price()
}
return cost
}
func (m *Meal) ShowItems() {
for _, item := range m.Items {
fmt.Println("Item : ", item.Name())
fmt.Print(", Packing : ", item.Packing().Pack())
fmt.Println(", Price : ", item.Price())
}
}
type MealBuilder struct {
}
func (m *MealBuilder) prepareVegMeal() *Meal {
meal := new(Meal)
meal.AddItem(new(VegBugger))
meal.AddItem(new(Coke))
return meal
}
func (m *MealBuilder) prepareNonVegMeal() *Meal {
meal := new(Meal)
meal.AddItem(new(ChickenBugger))
meal.AddItem(new(Pepsi))
return meal
}
func builderTest() {
mealBuilder := new(MealBuilder)
vegMeal := mealBuilder.prepareVegMeal()
fmt.Println("vegMeal")
vegMeal.ShowItems()
fmt.Println("Total Cost: " ,vegMeal.GetCost())
nonVegMeal := mealBuilder.prepareVegMeal()
fmt.Println("nonVegMeal")
nonVegMeal.ShowItems()
fmt.Println("Total Cost: " ,nonVegMeal.GetCost())
}