-
Notifications
You must be signed in to change notification settings - Fork 79
/
factory_method.go
59 lines (47 loc) · 957 Bytes
/
factory_method.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
package creational
import (
"fmt"
"io"
"os"
)
var outputWriter io.Writer = os.Stdout // modified during testing
// StoogeType is used as a enum for stooge types.
type StoogeType int
// Names of stooges as enums.
const (
Larry StoogeType = iota
Moe
Curly
)
// Stooge provides an interface for interacting with stooges.
type Stooge interface {
SlapStick()
}
type larry struct {
}
func (s *larry) SlapStick() {
fmt.Fprint(outputWriter, "Larry: Poke eyes\n")
}
type moe struct {
}
func (s *moe) SlapStick() {
fmt.Fprint(outputWriter, "Moe: Slap head\n")
}
type curly struct {
}
func (s *curly) SlapStick() {
fmt.Fprint(outputWriter, "Curly: Suffer abuse\n")
}
// NewStooge creates new stooges given the stooge type.
// Nil is returned if the stooge type is not recognised.
func NewStooge(stooge StoogeType) Stooge {
switch stooge {
case Larry:
return &larry{}
case Moe:
return &moe{}
case Curly:
return &curly{}
}
return nil
}