golang设计模式(3)组合模式

本文介绍了设计模式中的组合模式,它在处理树形结构问题时允许忽略简单元素和复杂元素的区别。组合模式使得客户端可以统一处理简单元素和复杂元素,从而解耦内部结构。在Golang中,这种模式被应用于web URL路由处理,通过目录分级实现路由匹配。文章通过定义Component、Leaf和Composite角色阐述了模式的应用,并提供了适用性的场景分析以及简单的Golang伪代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

组合模式,使我们在树形结构问题中,使用者可以忽略简单元素和复杂元素的概念。客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素内部结构解耦。当应用场景出现分级、分层的时候,使用组合模式就会相对合适。
定义(GoF《设计模式》):将对象组合成树形结构以表示“部分整体”的层次结构。组合模式使得用户对单个对象和使用具有一致性。
涉及角色:
  1.Component 是组合中的对象声明接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理Component
子部件。
  2.Leaf 在组合中表示叶子结点对象,叶子结点没有子结点。
  3.Composite 定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关操作。
  
适用性
  以下情况下适用Composite模式:
  1.你想表示对象的部分-整体层次结构
  2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
组合模式对应的uml图:
组合模式uml

golang实现:
笔者这边实际用到组合模式的情景是对web的url做对应的路由处理。url中的目录分级,将url按照‘/’切分后,有些是目录,最后一个是字符串就不是目录。伪代码如下:

package webapi

import (
    "errors"
    "fmt"
    "net/http"
    "strings"
    "sync"
)

type NodeHandler func(http.ResponseWriter, *http.Request)

type DirComponent interface {
    Add(DirComponent) bool
    Remove(string) bool
    GetChild() ([]DirComponent, bool)
    GetName() string
    Handler(w http.ResponseWriter, req *http.Request)
    IsDir() bool
}

type Node struct {
    Name        string
    Url         string
    NodeHandler NodeHandler
}

func NewNode(name string, handler NodeHandler) *Node {
    return &Node{Name: name, NodeHandler: handler}
}

func (pN *Node) Add(iDirComponent DirComponent) bool {
    return false
}

func (pN *Node) Remove(name string) bool {
    return false
}

func (pN *Node) GetChild() (sziDirComponent []DirComponent, b bool) {
    return
}

func (pN *Node) Handler(w http.ResponseWriter, req *http.Request) {
    pN.NodeHandler(w, req)
}

func (pN *Node) GetName() string {
    return pN.Name
}

func (pN *Node) IsDir() bool {
    return false
}

type Dir struct {
    Name  string
    Sons  []DirComponent
    mutex sync.Mutex
}

func NewDir(name string) *Dir {
    return &Dir{Name: name}
}

func (pD *Dir) Add(iDirComponent DirComponent) bool {
    pD.mutex.Lock()
    for _, dirComponent := range pD.Sons {
        if iDirComponent.GetName() == dirComponent.GetName() {
            return false
        }
    }
    pD.Sons = append(pD.Sons, iDirComponent)
    pD.mutex.Unlock()
    return true
}

func (pD *Dir) Remove(name string) bool {
    pD.mutex.Lock()
    for i, dirComponent := range pD.Sons {
        if name == dirComponent.GetName() {
            pD.Sons = append(pD.Sons[:i], pD.Sons[i+1:]...)
            pD.mutex.Unlock()
            return true
        }
    }
    pD.mutex.Unlock()
    return false
}

//dir in web must have child
func (pD *Dir) GetChild() (dirs []DirComponent, b bool) {
    if len(pD.Sons) == 0 {
        return dirs, false
    }
    return pD.Sons, true
}

func (pD *Dir) GetName() string {
    return pD.Name
}

func (pD *Dir) IsDir() bool {
    return true
}

//return error
func (pD *Dir) Handler(w http.ResponseWriter, req *http.Request) {
    return
}

func AddDir(url string, node DirComponent, root DirComponent) bool {
    if node == nil || root == nil || node.IsDir() {
        return false
    }

    dirs := strings.Split(url, "/")
    dirDeth := len(dirs)
    rootTmp := root

    for i, d := range dirs {
        if i == 0 {
            continue
        }

        if i == dirDeth-1 {
            sons, ok := rootTmp.GetChild()
            if ok {
                for _, dir := range sons {
                    if d == dir.GetName() {
                        return false
                    }
                }
            }
            fmt.Println("bbb")
            rootTmp.Add(node)
            return true
        }
        //获取子节点
        sons, ok := rootTmp.GetChild()
        if !ok {
            newdir := NewDir(d)
            rootTmp.Add(newdir)
            sons, _ = rootTmp.GetChild()
        }

        for _, dir := range sons {
            if d == dir.GetName() {
                rootTmp = dir
                break
            }
        }
    }
    return true
}

func DelDir(url string, root DirComponent) bool {
    if root == nil {
        return false
    }

    dirs := strings.Split(url, "/")
    dirDeth := len(dirs)
    rootTmp := root
    for i, d := range dirs {
        if i == 0 {
            continue
        }

        sons, ok := rootTmp.GetChild()
        if !ok {
            return false
        }

        for _, dir := range sons {
            if d == dir.GetName() {
                if i == dirDeth-1 {
                    fmt.Println("last")
                    return rootTmp.Remove(d)
                }
                rootTmp = dir
                break
            }
        }

    }

    return false
}

func UpdateDir(url string, node DirComponent, root DirComponent) bool {
    if DelDir(url, root) {
        return AddDir(url, node, root)
    }
    return false
}

func FindNode(url string, root DirComponent) (DirComponent, error) {
    if root == nil {
        return nil, errors.New("root is nil")
    }

    dirs := strings.Split(url, "/")
    dirDeth := len(dirs)
    rootTmp := root
    for i, d := range dirs {
        if i == 0 {
            continue
        }
        sons, ok := rootTmp.GetChild()
        if !ok {
            return nil, errors.New("dir no exist!")
        }

        for _, dir := range sons {
            if d == dir.GetName() {
                if i == dirDeth-1 {

                    return dir, nil
                }
                rootTmp = dir
                break
            }
        }
    }
    return nil, errors.New("no find!")
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值