網站首頁 編程語言 正文
抽象工廠模式
抽象工廠模式是一種創建型設計模式, 它能創建一系列相關的對象, 而無需指定其具體類。
抽象工廠定義了用于創建不同產品的接口, 但將實際的創建工作留給了具體工廠類。 每個工廠類型都對應一個特定的產品變體。
在創建產品時, 客戶端代碼調用的是工廠對象的構建方法, 而不是直接調用構造函數 (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
相關推薦
- 2022-05-20 如何搭建雙 M 結構的主從備份?
- 2022-06-30 python神經網絡MobileNetV2模型的復現詳解_python
- 2022-07-11 Linux刪除某個字母開頭的所有文件
- 2024-04-04 netty使用http和webSocket
- 2023-07-24 表單默認空白,無數據時賦默認值,新增時賦默認值
- 2022-05-22 jquery對標簽添加只讀(readonly)或者禁用(disabled)屬性_jquery
- 2023-10-09 使用Double Toke登錄的優點
- 2022-12-14 C語言程序設計之指針的應用詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支