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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Golang設(shè)計(jì)模式中抽象工廠模式詳細(xì)講解_Golang

作者:Ch3n ? 更新時(shí)間: 2023-02-27 編程語(yǔ)言

抽象工廠模式

抽象工廠模式是一種創(chuàng)建型設(shè)計(jì)模式, 它能創(chuàng)建一系列相關(guān)的對(duì)象, 而無(wú)需指定其具體類。

抽象工廠定義了用于創(chuàng)建不同產(chǎn)品的接口, 但將實(shí)際的創(chuàng)建工作留給了具體工廠類。 每個(gè)工廠類型都對(duì)應(yīng)一個(gè)特定的產(chǎn)品變體。

在創(chuàng)建產(chǎn)品時(shí), 客戶端代碼調(diào)用的是工廠對(duì)象的構(gòu)建方法, 而不是直接調(diào)用構(gòu)造函數(shù) (new操作符)。 由于一個(gè)工廠對(duì)應(yīng)一種產(chǎn)品變體, 因此它創(chuàng)建的所有產(chǎn)品都可相互兼容。

客戶端代碼僅通過(guò)其抽象接口與工廠和產(chǎn)品進(jìn)行交互。 該接口允許同一客戶端代碼與不同產(chǎn)品進(jìn)行交互。 你只需創(chuàng)建一個(gè)具體工廠類并將其傳遞給客戶端代碼即可。

概念示例

讓我們假設(shè)一下, 如果你想要購(gòu)買(mǎi)一組運(yùn)動(dòng)裝備, 比如一雙鞋與一件襯衫這樣由兩種不同產(chǎn)品組合而成的套裝。 相信你會(huì)想去購(gòu)買(mǎi)同一品牌的商品, 這樣商品之間能夠互相搭配起來(lái)。

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

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: 抽象產(chǎn)品

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: 具體產(chǎn)品

package main
type AdidasShoe struct {
    Shoe
}

nikeShoe.go: 具體產(chǎn)品

package main
type NikeShoe struct {
    Shoe
}

iShirt.go: 抽象產(chǎn)品

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: 具體產(chǎn)品

package main
type AdidasShirt struct {
    Shirt
}

nikeShirt.go: 具體產(chǎn)品

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: 執(zhí)行結(jié)果

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

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

欄目分類
最近更新