網(wǎng)站首頁(yè) 編程語(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
相關(guān)推薦
- 2022-07-30 SpringBoot的數(shù)據(jù)校驗(yàn)(@Validated注解)、關(guān)于validation無(wú)法導(dǎo)入的問(wèn)題解
- 2023-05-12 Oracle中實(shí)現(xiàn)刪除重復(fù)數(shù)據(jù)只保留一條_oracle
- 2022-11-16 python?中collections的?deque使用詳解_python
- 2022-01-23 win7下與Virtualbox下的ubuntu11.04共享文件夾
- 2022-07-20 C語(yǔ)言實(shí)例講解嵌套語(yǔ)句的用法_C 語(yǔ)言
- 2023-02-07 基于C#實(shí)現(xiàn)磁性吸附窗體_C#教程
- 2022-11-21 如何使用正則表達(dá)式對(duì)輸入數(shù)字進(jìn)行匹配詳解_正則表達(dá)式
- 2022-05-01 Entity?Framework系統(tǒng)架構(gòu)與原理介紹_基礎(chǔ)應(yīng)用
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支