《golang设计模式》第二部分·结构型模式-03-组合模式(Composite)

1. 概述

组合(Composite)是指使用组合和继承关系将聚合体及其组成元素分解成树状结构,以便客户端在不需要区分聚合体或组成元素类型的情况下使用统一的接口操作它们。

例如:将叶子节点和复合节点组合起来,定义一个抽象接口遍历他们

1.1 角色

  • Component(抽象构件):为叶子构件和复合构件声明接口,定义了结构树的操作,如整个结构的遍历等。
  • Leaf(叶子节点):叶子节点没有子节点。
  • Composite(复合节点):
    • 可以包含叶节点或其他复合节点。
    • 包含管理子节点的方法,如子节点的增加、删除、查询等。

1.2 类图

«interface»
Component
+Service()
Leaf
+Service()
Composite
+[]Component
+Service()
+Add(c Component)
+Remove(c Component)
+GetChild(c Component)

2. 代码示例

2.1 设计

  • 定义抽象构建Node
    • 它包含Get()方法遍历自己
  • 定义叶子节点FileNode
    • 它的Get()方法实现Node接口
    • 它的SetName()方法设置它的题目
    • 它的SetContent()方法设置它的内容
  • 定义复合节点DirNode
    • 它的Get()方法实现Node接口
    • 它的SetName()方法设置它的题目
    • 它的AddNode方法给添加子节点
    • 当然你也可以加删除等方法
  • 调用
    • 实例化一些复合节点和一些叶子节点
    • 将他们用复合节点的AddNode方法组合起来
    • 使用Get()方法遍历组装后的树结构

2.2 代码

package main

import "fmt"
//定义一个抽象构建
type Node interface {
	Get(separator string)
	//SetName(name string)
}
//定义叶子节点
type FileNode struct {
	Name    string
	Content string
}
//它的查询方法实现接口
func (fileNode *FileNode) Get(separator string) {
	fmt.Printf("%s %q内容为:%s\n", separator, fileNode.Name, fileNode.Content)
}
//设置叶子节点内容
func (fileNode *FileNode) SetContent(content string) {
	fileNode.Content = content
}
//设置叶子节点标题
func (fileNode *FileNode) SetName(name string) {
	fileNode.Name = name
}
//定义复合节点
type DirNode struct {
	Name     string
	Contents []Node
}
//它的查询方法实现抽象接口
func (dirNode *DirNode) Get(separator string) {
	fmt.Printf("%s %q\n", separator, dirNode.Name)
	for _, node := range dirNode.Contents {
		node.Get(separator + separator)
	}
}
//设置复合节点的名字
func (dirNode *DirNode) SetName(name string) {
	dirNode.Name = name
}
//给复合节点加入子节点
func (dirNode *DirNode) AddNod(node Node) {
	dirNode.Contents = append(dirNode.Contents, node)
}

func main() {
	//实例化两个目录
	note := DirNode{}
	k8s := DirNode{}
	//实例化两个文件
	deployment := FileNode{}
	daemonSet := FileNode{}
	//设置目录名
	note.SetName("xuandeNode")
	k8s.SetName("k8s")
	//设置文件名和文件内容
	deployment.SetName("deployment")
	daemonSet.SetName("daemonSet")
	deployment.SetContent("balabalabala")
	daemonSet.SetContent("balabalabala")
	//组装目录
	note.AddNod(&k8s)
	k8s.AddNod(&deployment)
	k8s.AddNod(&daemonSet)
	//查看组装结果
	note.Get("----")
}
  • 输出
---- "xuandeNode"
-------- "k8s"
---------------- "deployment"内容为:balabalabala
---------------- "daemonSet"内容为:balabalabala 

2.3 类图

«interface»
NodeOption
+Get(separator string)
FileNode
+String Content
+Get(separator string)
+SetContent(content string)
+SetName(name string)
DirNode
+[]NodeOption Contents
+Get(separator string)
+SetName(name string)
+AddNod(node NodeOption)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是几种常见的Golang设计模式: 1. 工厂模式(Factory Pattern):用于创建对象的模式,通过定义一个创建对象的接口来实现对象的实例化。 ```go type Shape interface { Draw() } type Circle struct{} func (c *Circle) Draw() { fmt.Println("Drawing a circle") } type Rectangle struct{} func (r *Rectangle) Draw() { fmt.Println("Drawing a rectangle") } type ShapeFactory struct{} func (sf *ShapeFactory) GetShape(shapeType string) Shape { if shapeType == "circle" { return &Circle{} } else if shapeType == "rectangle" { return &Rectangle{} } return nil } func main() { factory := &ShapeFactory{} circle := factory.GetShape("circle") circle.Draw() // 输出:Drawing a circle rectangle := factory.GetShape("rectangle") rectangle.Draw() // 输出:Drawing a rectangle } ``` 2. 单例模式(Singleton Pattern):确保一个类只有一个实例,并提供一个全局访问点。 ```go type Singleton struct{} var instance *Singleton func GetInstance() *Singleton { if instance == nil { instance = &Singleton{} } return instance } func main() { singleton1 := GetInstance() singleton2 := GetInstance() fmt.Println(singleton1 == singleton2) // 输出:true } ``` 3. 观察者模式(Observer Pattern):定义了一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都会得到通知并自动更新。 ```go type Subject struct { observers []Observer } func (s *Subject) Attach(observer Observer) { s.observers = append(s.observers, observer) } func (s *Subject) Notify() { for _, observer := range s.observers { observer.Update() } } type Observer interface { Update() } type ConcreteObserver struct{} func (co *ConcreteObserver) Update() { fmt.Println("Observer is updated") } func main() { subject := &Subject{} observer := &ConcreteObserver{} subject.Attach(observer) subject.Notify() // 输出:Observer is updated } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄德公笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值