Здесь я пытаюсь изучить шаблон абстрактной фабрики. Ссылались на несколько примеров и пытались имитировать то же самое с некоторыми примерами фиктивных серверов. Пожалуйста, помогите мне просмотреть это.
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
type server interface {
Query()
GetContent() string
}
type google struct {
URL string
content string
}
type yahoo struct {
URL string
content string
}
func (g *google) Query() {
resp, err := http.Get(g.URL)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
g.content = string(body)
}
func (g *google) GetContent() string {
return g.content
}
func (g *yahoo) Query() {
resp, err := http.Get(g.URL)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
g.content = string(body)
}
func (g *yahoo) GetContent() string {
return g.content
}
func createFactory(domain string) server {
switch domain {
case "google":
return &google{URL: "https://www.google.com"}
case "yahoo":
return &yahoo{URL: "https://www.yahoo.com"}
default:
fmt.Println("Invalid domain")
return nil
}
}
func main() {
googleServer := createFactory("google")
googleServer.Query()
fmt.Println(googleServer.GetContent())
yahooServer := createFactory("yahoo")
yahooServer.Query()
fmt.Println(googleServer.GetContent())
}