design model 之 subcribe/publish

简单工厂模式

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
type sayer interface {
say()
}

type man struct{

}

func (m *man)say(){
fmt.Printf("I am man")
}

type woman struct{

}

func (w *woman)say(){
fmt.Printf("I am woman")
}

func factory(t string) sayer{
if t=="man" {
return &man{}
}else{
return &woman{}
}
}

func main(){
s := factory("man")
s.say()
}