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

文章目录

  • [1. 概述](#1. 概述)
    • [1.1 角色](#1.1 角色)
    • [1.2 类图](#1.2 类图)
  • [2. 代码示例](#2. 代码示例)
    • [2.1 设计](#2.1 设计)
    • [2.2 代码](#2.2 代码)
    • [2.3 类图](#2.3 类图)

1. 概述

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

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

go 复制代码
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("----")
}
  • 输出
shell 复制代码
---- "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)


相关推荐
杉氧17 小时前
深入理解 Compose 重组机制:快照系统如何驱动 UI 精准刷新?
android·架构·android jetpack
杉氧18 小时前
深度解析:Jetpack Compose 核心架构与底层原理 —— 十年安卓老兵的“破茧重生”
android·架构·android jetpack
Lion0918 小时前
ReAct 循环:Agent 的思考引擎 — Think → Act → Observe
架构
得物技术20 小时前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
自珍JAVA1 天前
Superpowers AI编码秩序
架构
古茗前端团队1 天前
急招!前端|测试|后端|产品(名额多,速来)
前端·后端·架构
木雷坞1 天前
我再也不敢随手 `docker compose down -v` 了
架构
胡萝卜术1 天前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
没落英雄1 天前
从零开始搭建一个 AI Agent —— LangChain + TypeScript 实战手记
前端·人工智能·架构
doiito1 天前
【Agent Harness】Gliding Horse 设计细节 -- 不跟风开发自己的AI Agent
架构·rust·agent