package main
import “fmt”
type Product interface {
GetName() string
}
type ProductA struct{}
func (p *ProductA) GetName() string {
return "Product A"
}
type ProductB struct{}
func (p *ProductB) GetName() string {
return "Product B"
}
type Factory struct{}
func (f *Factory) CreateProduct(productType string) Product {
switch productType {
case "A":
return &ProductA{}
case "B":
return &ProductB{}
default:
return nil
}
}
func main() {
factory := &Factory{}
productA := factory.CreateProduct("A")
fmt.Println(productA.GetName())
productB := factory.CreateProduct("B")
fmt.Println(productB.GetName())
}