viviman 4 years ago
parent
commit
b8fbd85b66
  1. 41
      golang_learn/data_prattern/abstract_factory/main.go
  2. 9
      golang_learn/data_prattern/abstract_factory/main_test.go
  3. 33
      golang_learn/data_prattern/factory/main.go
  4. 9
      golang_learn/data_prattern/factory/main_test.go

41
golang_learn/data_prattern/abstract_factory/main.go

@ -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{}
}

9
golang_learn/data_prattern/abstract_factory/main_test.go

@ -0,0 +1,9 @@
package abstract_factory
import "testing"
func TestNewSimpleLunchFactory(t *testing.T) {
NewSimpleLunchFactory().CreateFood().Cook()
NewSimpleLunchFactory().CreateVegetable().Cook()
}

33
golang_learn/data_prattern/factory/main.go

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

9
golang_learn/data_prattern/factory/main_test.go

@ -0,0 +1,9 @@
package factory
import "testing"
func TestNewRestaurant(t *testing.T) {
NewRestaurant("Aa").GetFood()
NewRestaurant("Bb").GetFood()
}
Loading…
Cancel
Save