<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送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 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看