<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] ## 要點 裝飾器生成和組件相同的方法 ## 示例 ### demo ### 裝飾模式(添加鉤子) 格式: <details> <summary>main.go</summary> ``` package main import "fmt" //定義抽象的組件 type Component interface { Operate() } type Component1 struct { } func (c Component1) Operate() { fmt.Printf("%+v\n", "c1 operate") } // 定義一個抽象的裝飾者 type Decorator interface { Component Do() //裝飾行為,可以有多個 } //定義一個具體的裝飾器 type Decorator1 struct { c Component } func (d Decorator1) Do() { fmt.Printf("%+v\n", "c1 發生了裝飾行為") } // 重新實現component 的方法 func (d Decorator1) Operate() { d.Do() d.c.Operate() d.Do() } func main() { //無裝飾模式 c1:=&Component1{} c1.Operate() //裝飾模式 c2 :=&Decorator1{ c: c1, } c2.Operate() } ``` </details> <br /> ### 披薩價格 <details> <summary>main.go</summary> ``` package main import "fmt" // 零件接口 type pizza interface { getPrice() int } // 具體零件 type veggeMania struct { } func (p *veggeMania) getPrice() int { return 15 } // 具體裝飾 type tomatoTopping struct { pizza pizza } func (c *tomatoTopping) getPrice() int { pizzaPrice := c.pizza.getPrice() return pizzaPrice + 7 } // 具體裝飾 type cheeseTopping struct { pizza pizza } func (c *cheeseTopping) getPrice() int { pizzaPrice := c.pizza.getPrice() return pizzaPrice + 10 } // 客戶端代碼 func main() { pizza := &veggeMania{} pizzaWithCheese := &cheeseTopping{ pizza: pizza, } pizzaWithCheeseAndTomato := &tomatoTopping{ pizza: pizzaWithCheese, } fmt.Printf("披薩價格: %d\n", pizzaWithCheeseAndTomato.getPrice()) } ``` </details> <br /> 輸出 ``` 披薩價格: 32 ```
                  <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>

                              哎呀哎呀视频在线观看