go-pattern-examples/creation/02_simple_factory/simple.go
2020-05-02 17:36:50 +08:00

43 lines
777 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
}