-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsimple.go
42 lines (33 loc) · 777 Bytes
/
simple.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
package simplefactory
import "fmt"
type schoolmember int
const (
student schoolmember = iota
teacher
)
//Mouth is interface that people can Say words
type Mouth interface {
Say(name string) string
}
//New return instance object that have `Mouth` and can speak
func New(t schoolmember) Mouth {
switch t {
case student:
return &studentType{}
case teacher:
return &teacherType{}
}
return nil
}
//teacherType is one of Mouth implement
type teacherType struct{}
//Say teacher's name
func (*teacherType) Say(name string) string {
return fmt.Sprintf("I am Teacher: %s", name)
}
//studentType is another Mouth implement
type studentType struct{}
//Say student's name
func (*studentType) Say(name string) string {
return fmt.Sprintf("I am Student: %s", name)
}