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