-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorator.go
44 lines (35 loc) · 928 Bytes
/
decorator.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
package main
import "fmt"
/*
Allows adding new functionality to an existing object without changing its structure.
This type of design pattern is a structural pattern, which acts as a wrapper around an existing class.
imagine a person put on different kinds of clothes which can be taken off or put on.
contains:
1. basic component
2. several layers share same behaviours
*/
type pizza interface {
getPrice() int
}
type veggeMania struct{}
func (v *veggeMania) getPrice() int {
return 10
}
type tomatoTopping struct {
pizza
}
func (t *tomatoTopping) getPrice() int {
return t.pizza.getPrice() + 3
}
type cheeseTopping struct {
pizza
}
func (c *cheeseTopping) getPrice() int {
return c.pizza.getPrice() + 5
}
func RunDecorator() {
pizzaWithTomatoAndCheese := &cheeseTopping{
&tomatoTopping{&veggeMania{}},
}
fmt.Printf("pizza with tomato and cheese costs $%d \n", pizzaWithTomatoAndCheese.getPrice())
}