ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 示例 ### 概念示例 <details> <summary>main.go</summary> ``` package main import "fmt" // 抽象产品 type IProduct interface { SetName(string) GetName() string } // 具体产品 type Product1 struct { name string } func (p *Product1) SetName(name string) { p.name=name } func (p Product1) GetName() string { return "Product1's name "+p.name } // 具体产品 type Product2 struct { name string } func (p *Product2) SetName(name string) { p.name=name } func (p Product2) GetName() string { return "Product2's name "+p.name } //实现一个抽象工厂 type IProductFactory interface { Create() IProduct } //实现一个具体的工:产品1的工厂 type ProductFactory1 struct { } func (p *ProductFactory1) Create() IProduct { return &Product1{} } //实现一个具体的工:产品2的工厂 type ProductFactory2 struct { } func (p *ProductFactory2) Create() IProduct { return &Product2{} } func main() { productFactory:=ProductFactory1{} //productFactory:=&ProductFactory2{} product := productFactory.Create() printProduct(product) } func printProduct(product IProduct){ product.SetName("p1") fmt.Printf("%+v\n", product.GetName()) } ``` </details> <br /> 输出 ``` Product1's name p1 ``` ### 运动装配示例 如果你想要购买一组运动装备, 比如一双鞋与一件衬衫这样由两种不同产品组合而成的套装 <details> <summary>main.go</summary> ``` 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") } // 具体工厂 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, }, } } // 具体工厂 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, }, } } // 抽象产品 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 } // 具体产品 type adidasShoe struct { shoe } // 具体产品 type nikeShoe struct { shoe } // 抽象产品 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 } // 具体产品 type adidasShirt struct { shirt } // 具体产品 type nikeShirt struct { shirt } 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() } ``` </details> <br /> 输出 ``` Logo: nike Size: 14 Logo: nike Size: 14 Logo: adidas Size: 14 Logo: adidas Size: 14 ``` ### 缓存示例 <details> <summary>main.go</summary> ``` package main import ( "fmt" ) type ICache interface { Set(key,value string) Get(key string) string } type RedisCache struct { data map[string]string } func (r *RedisCache) Set(key, value string) { r.data[key]=value } func (r RedisCache) Get(key string) string { return r.data[key] } func NewRedisCache() *RedisCache{ return &RedisCache{ data: make(map[string]string), } } type Memcached struct { data map[string]string } func (r *Memcached) Set(key, value string) { r.data[key]=value } func (r Memcached) Get(key string) string { return r.data[key] } func NewMemcached() *Memcached{ return &Memcached{ data: make(map[string]string), } } type CacheFactory interface { Create() ICache } //具体redis 工厂 type RedisCacheFactory struct { } func (r RedisCacheFactory) Create() ICache { return NewRedisCache() } //具体memcached 工厂 type MemCachedFactory struct { } func (r MemCachedFactory) Create() ICache { return NewMemcached() } func main() { CacheFactory :=&RedisCacheFactory{} //CacheFactory:=&MemCachedFactory{} cache := CacheFactory.Create() printCache(cache) } func printCache(cache ICache){ cache.Set("a","1") fmt.Printf("%+v\n", cache.Get("a")) } ``` </details> <br /> 输出 ``` 1 ```