日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Golang設計模式中抽象工廠模式詳細講解_Golang

作者:Ch3n ? 更新時間: 2023-02-27 編程語言

抽象工廠模式

抽象工廠模式是一種創建型設計模式, 它能創建一系列相關的對象, 而無需指定其具體類。

抽象工廠定義了用于創建不同產品的接口, 但將實際的創建工作留給了具體工廠類。 每個工廠類型都對應一個特定的產品變體。

在創建產品時, 客戶端代碼調用的是工廠對象的構建方法, 而不是直接調用構造函數 (new操作符)。 由于一個工廠對應一種產品變體, 因此它創建的所有產品都可相互兼容。

客戶端代碼僅通過其抽象接口與工廠和產品進行交互。 該接口允許同一客戶端代碼與不同產品進行交互。 你只需創建一個具體工廠類并將其傳遞給客戶端代碼即可。

概念示例

讓我們假設一下, 如果你想要購買一組運動裝備, 比如一雙鞋與一件襯衫這樣由兩種不同產品組合而成的套裝。 相信你會想去購買同一品牌的商品, 這樣商品之間能夠互相搭配起來。

如果我們把這樣的行為轉換成代碼的話, 幫助我們創建此類產品組的工具就是抽象工廠, 便于產品之間能夠相互匹配。

iSportsFactory.go: 抽象工廠接口

package main
import "fmt"
type ISportsFactory interface {
    makeShoe() IShoe
    makeShirt() IShirt
}
func GetSportsFactory(brand string) (ISportsFactory, error) {
    if brand == "adidas" {
        return &Adidas{}, nil
    }
    if brand == "nike" {
        return &Nike{}, nil
    }
    return nil, fmt.Errorf("Wrong brand type passed")
}

adidas.go: 具體工廠

package main
type Adidas struct {
}
func (a *Adidas) makeShoe() IShoe {
    return &AdidasShoe{
        Shoe: Shoe{
            logo: "adidas",
            size: 14,
        },
    }
}
func (a *Adidas) makeShirt() IShirt {
    return &AdidasShirt{
        Shirt: Shirt{
            logo: "adidas",
            size: 14,
        },
    }
}

nike.go: 具體工廠

package main
type Nike struct {
}
func (n *Nike) makeShoe() IShoe {
    return &NikeShoe{
        Shoe: Shoe{
            logo: "nike",
            size: 14,
        },
    }
}
func (n *Nike) makeShirt() IShirt {
    return &NikeShirt{
        Shirt: Shirt{
            logo: "nike",
            size: 14,
        },
    }
}

iShoe.go: 抽象產品

package main
type IShoe interface {
    setLogo(logo string)
    setSize(size int)
    getLogo() string
    getSize() int
}
type Shoe struct {
    logo string
    size int
}
func (s *Shoe) setLogo(logo string) {
    s.logo = logo
}
func (s *Shoe) getLogo() string {
    return s.logo
}
func (s *Shoe) setSize(size int) {
    s.size = size
}
func (s *Shoe) getSize() int {
    return s.size
}

adidasShoe.go: 具體產品

package main
type AdidasShoe struct {
    Shoe
}

nikeShoe.go: 具體產品

package main
type NikeShoe struct {
    Shoe
}

iShirt.go: 抽象產品

package main
type IShirt interface {
    setLogo(logo string)
    setSize(size int)
    getLogo() string
    getSize() int
}
type Shirt struct {
    logo string
    size int
}
func (s *Shirt) setLogo(logo string) {
    s.logo = logo
}
func (s *Shirt) getLogo() string {
    return s.logo
}
func (s *Shirt) setSize(size int) {
    s.size = size
}
func (s *Shirt) getSize() int {
    return s.size
}

adidasShirt.go: 具體產品

package main
type AdidasShirt struct {
    Shirt
}

nikeShirt.go: 具體產品

package main
type NikeShirt struct {
    Shirt
}

main.go: 客戶端代碼

package main
import "fmt"
func main() {
    adidasFactory, _ := GetSportsFactory("adidas")
    nikeFactory, _ := GetSportsFactory("nike")
    nikeShoe := nikeFactory.makeShoe()
    nikeShirt := nikeFactory.makeShirt()
    adidasShoe := adidasFactory.makeShoe()
    adidasShirt := adidasFactory.makeShirt()
    printShoeDetails(nikeShoe)
    printShirtDetails(nikeShirt)
    printShoeDetails(adidasShoe)
    printShirtDetails(adidasShirt)
}
func printShoeDetails(s IShoe) {
    fmt.Printf("Logo: %s", s.getLogo())
    fmt.Println()
    fmt.Printf("Size: %d", s.getSize())
    fmt.Println()
}
func printShirtDetails(s IShirt) {
    fmt.Printf("Logo: %s", s.getLogo())
    fmt.Println()
    fmt.Printf("Size: %d", s.getSize())
    fmt.Println()
}

output.txt: 執行結果

Logo: nike
Size: 14
Logo: nike
Size: 14
Logo: adidas
Size: 14
Logo: adidas
Size: 14

原文鏈接:https://ch3nnn.blog.csdn.net/article/details/126847391

欄目分類
最近更新