-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathfactory.go
70 lines (54 loc) · 793 Bytes
/
factory.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
package creational
type Gun interface {
Name() string
Power() float64
}
type AK47 struct {
}
type A92F struct {
}
type Fist struct {
}
type GunType uint
const (
FistType GunType = 1 << iota
RifleType
PistolType
)
func NewGun(t GunType) Gun {
switch t {
case RifleType:
return newAK47()
case PistolType:
return newA92F()
default:
return newFist()
}
}
func (a *AK47) Name() string {
return "AK47"
}
func (a *AK47) Power() float64 {
return 30.0
}
func (a *A92F) Name() string {
return "A92F"
}
func (a *A92F) Power() float64 {
return 10.0
}
func (f *Fist) Name() string {
return "Fist"
}
func (f *Fist) Power() float64 {
return 0.01
}
func newAK47() *AK47 {
return &AK47{}
}
func newA92F() *A92F {
return &A92F{}
}
func newFist() *Fist {
return &Fist{}
}