<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] ## 組合模式 一個具有層級關系的對象由一系列擁有父子關系的對象通過樹形結構組成(類似樹形式的組件) > [參考](http://tigerb.cn/2020/04/06/go-patterns-component/?utm_source=tuicool&utm_medium=referral) > ![UTOOLS1589241072846.png](http://yanxuan.nosdn.127.net/5a119611178cd768ade28de3c7ad4bc9.png) ``` 成員屬性 ChildComponents: 子組件列表 -> 穩定不變的 成員方法 Mount: 添加一個子組件 -> 穩定不變的 Remove: 移除一個子組件 -> 穩定不變的 Do: 執行組件&子組件 -> 變化的 ``` <details> <summary>main.go</summary> ``` package main import ( "context" "fmt" "reflect" "runtime" ) // Context 上下文 //type Context struct{} // IComponent 組件接口 type IComponent interface { // 添加一個子組件 Mount(c IComponent, components ...IComponent) error // 移除一個子組件 Remove(c IComponent) error // 執行組件&子組件 Do(ctx context.Context) error } // BaseComponent 基礎組件 // 實現Add:添加一個子組件 // 實現Remove:移除一個子組件 type BaseComponent struct { // 子組件列表 ChildComponents []IComponent } // Mount 掛載一個子組件 func (bc *BaseComponent) Mount(c IComponent, components ...IComponent) (err error) { bc.ChildComponents = append(bc.ChildComponents, c) if len(components) == 0 { return } bc.ChildComponents = append(bc.ChildComponents, components...) return } // Remove 移除一個子組件 func (bc *BaseComponent) Remove(c IComponent) (err error) { if len(bc.ChildComponents) == 0 { return } for k, childComponent := range bc.ChildComponents { if c == childComponent { fmt.Println(runFuncName(), "移除:", reflect.TypeOf(childComponent)) bc.ChildComponents = append(bc.ChildComponents[:k], bc.ChildComponents[k+1:]...) } } return } // Do 執行組件&子組件 func (bc *BaseComponent) Do(ctx context.Context) (err error) { // Do nothing return } // ChildsDo 執行子組件 func (bc *BaseComponent) ChildsDo(ctx context.Context) (err error) { // 執行子組件 for _, childComponent := range bc.ChildComponents { if err = childComponent.Do(ctx); err != nil { return err } } return } type All struct { BaseComponent } type A1 struct { BaseComponent } func (a *A1) Do(ctx context.Context) (err error) { fmt.Println(runFuncName(), "A1...") a.ChildsDo(ctx) return } type A11 struct { BaseComponent } func (a *A11) Do(ctx context.Context) (err error) { fmt.Println(runFuncName(), "A11...") a.ChildsDo(ctx) return } type A12 struct { BaseComponent } func (a *A12) Do(ctx context.Context) (err error) { fmt.Println(runFuncName(), "A12...") a.ChildsDo(ctx) return } type B1 struct { BaseComponent } func (a *B1) Do(ctx context.Context) (err error) { fmt.Println(runFuncName(), "B1...") a.ChildsDo(ctx) return } type C1 struct { BaseComponent } func (a *C1) Do(ctx context.Context) (err error) { fmt.Println(runFuncName(), "C1...") a.ChildsDo(ctx) fmt.Printf("%+v\n", ctx.Value("a")) return } func main() { // 初始化訂單結算頁面 這個大組件 all :=&All{} a1 := &A1{} a1.Mount(&A11{}, &A12{}) b1 := &B1{} all.Mount(a1, b1,&C1{}) // 只能刪除本身的子組件 all.Remove(b1) // 開始構建頁面組件數據 ctx := context.WithValue(context.Background(), "a", "b") all.ChildsDo(ctx) } // 獲取正在運行的函數名 func runFuncName() string { pc := make([]uintptr, 1) runtime.Callers(2, pc) f := runtime.FuncForPC(pc[0]) return f.Name() } ``` </details> <br/>
                  <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>

                              哎呀哎呀视频在线观看