forked from go/golangs_learn
viviman
4 years ago
4 changed files with 92 additions and 0 deletions
@ -0,0 +1,41 @@ |
|||||
|
package abstract_factory |
||||
|
|
||||
|
import "fmt" |
||||
|
|
||||
|
type Lunch interface { |
||||
|
Cook() |
||||
|
} |
||||
|
|
||||
|
type Rise struct { |
||||
|
} |
||||
|
|
||||
|
func (r *Rise) Cook() { |
||||
|
fmt.Println("这是 测试1111。") |
||||
|
} |
||||
|
|
||||
|
type Tomato struct { |
||||
|
} |
||||
|
|
||||
|
func (t *Tomato) Cook() { |
||||
|
fmt.Println("这是 测试22222。") |
||||
|
} |
||||
|
|
||||
|
type LunchFactory interface { |
||||
|
CreateFood() Lunch |
||||
|
CreateVegetable() Lunch |
||||
|
} |
||||
|
|
||||
|
type SimpleLunchFactory struct { |
||||
|
} |
||||
|
|
||||
|
func NewSimpleLunchFactory() LunchFactory { |
||||
|
return &SimpleLunchFactory{} |
||||
|
} |
||||
|
|
||||
|
func (s *SimpleLunchFactory) CreateFood() Lunch { |
||||
|
return &Rise{} |
||||
|
} |
||||
|
|
||||
|
func (s *SimpleLunchFactory) CreateVegetable() Lunch { |
||||
|
return &Tomato{} |
||||
|
} |
@ -0,0 +1,9 @@ |
|||||
|
package abstract_factory |
||||
|
|
||||
|
import "testing" |
||||
|
|
||||
|
func TestNewSimpleLunchFactory(t *testing.T) { |
||||
|
|
||||
|
NewSimpleLunchFactory().CreateFood().Cook() |
||||
|
NewSimpleLunchFactory().CreateVegetable().Cook() |
||||
|
} |
@ -0,0 +1,33 @@ |
|||||
|
package factory |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
) |
||||
|
|
||||
|
type Restaurant interface { |
||||
|
GetFood() |
||||
|
} |
||||
|
|
||||
|
type Aa struct { |
||||
|
} |
||||
|
|
||||
|
type Bb struct { |
||||
|
} |
||||
|
|
||||
|
func (a *Aa) GetFood() { |
||||
|
fmt.Println("Aa ... 测试工厂生产, ", &a) |
||||
|
} |
||||
|
|
||||
|
func (b *Bb) GetFood() { |
||||
|
fmt.Println("Bb ... 测试工厂生产, ", &b) |
||||
|
} |
||||
|
|
||||
|
func NewRestaurant(name string) Restaurant { |
||||
|
switch name { |
||||
|
case "Aa": |
||||
|
return &Aa{} |
||||
|
case "Bb": |
||||
|
return &Bb{} |
||||
|
} |
||||
|
return nil |
||||
|
} |
@ -0,0 +1,9 @@ |
|||||
|
package factory |
||||
|
|
||||
|
import "testing" |
||||
|
|
||||
|
func TestNewRestaurant(t *testing.T) { |
||||
|
|
||||
|
NewRestaurant("Aa").GetFood() |
||||
|
NewRestaurant("Bb").GetFood() |
||||
|
} |
Loading…
Reference in new issue