Golang的类图
目录
1. 依赖(Dependency)
1.1 概念
类与类的链接,A依赖于B,B的变化引起A的变化。
go中表现为B是A的方法的参数。
1.2 代码示例
- 定义两个结构体
植物
和天气
- 定义
植物
的方法生长
,它需要参数天气
- 实例化
植物
和天气
,调用植物的生长
方法,将天气
作为参数传入得到结果。 - 我们可以看到:
天气
是植物
的依赖
package main import "fmt" type Plant struct { Leaves string Trunk string Root string } type Weather struct { Temperature int64 Raining bool } func (plant *Plant) Grow(weather *Weather) (result string) { if weather.Raining == true && weather.Temperature > 18 { return "植物生长了" } return "植物没有生长" } func main() { apple := Plant{ Leaves: "阔叶", } weather := &Weather{ Temperature: 20, Raining: true, } fmt.Println(apple.Grow(weather)) }
输出
植物生长了
1.3 类图示例
上边的代码创建类图如下:
2. 泛化(Generalization)-继承
2.1 概念
表现为类与类或接口与接口之间的继承关系。
go中表现为结构体的伪继承。
2.2 代码示例
- 上例中,我们添加一个结构体
水果
,其中一个成员引用结构体植物
- 实例化
苹果
和一个天气
植物
类的方法苹果
依然可以实现- 我们可以看到:
水果
类伪继承了植物
类
package main import "fmt" type Plant struct { Leaves string Trunk string Root string } type Fruit struct { Name string Plant } type Weather struct { Temperature int64 Raining bool } func (plant *Plant) Grow(weather *Weather) (result string) { if weather.Raining == true && weather.Temperature > 18 { return "植物生长了" } return "植物没有生长" } func main() { apple := Fruit{ Name: "苹果", } weather := &Weather{ Temperature: 20, Raining: true, } fmt.Println(apple.Grow(weather)) }
2.3 类图
3. 泛化(Generalization)-实现
3.1 概念
表现为类对接口的实现
3.2 代码示例
- 定义一个结构体
植物
- 定义一个
植物
类的方法生长
- 定义一个
变化
接口,封装了生长
方法 - 实例化
苹果
,实现变化
接口。 - 我们可以看到:类
植物
的实例化苹果
,实现了变化
接口
package main import "fmt" type Plant struct { Name string } type Weather struct { Temperature int64 Raining bool } func (plant *Plant) Grow(weather *Weather) (result string) { if weather.Raining == true && weather.Temperature > 18 { return "植物生长了" } return "植物没有生长" } type Change interface { Grow(weather *Weather) (result string) } func main() { plant := &Plant{ Name: "orange", } weather := &Weather{ Temperature: 20, Raining: true, } result := Change(plant).Grow(weather) fmt.Println(result) }
3.3 类图
4. 关联关系(Association)
4.1 概念
表现为变量
- 单向关联
只有一个类知道另外一个类的公共属性和操作 - 双向关联
两个类都知道另一个类的公共属性和操作
4.2 代码示例
植物
结构体中关联了天气
- 为
植物
定义一个生长
方法,改方法将天气
作为变量:当温度>20 且下雨的时候植物生长 - 实例化一个“植物”——“苹果”,实例化一个天气
- 第一天,根据
天气
判断植物生长 - 第二天,
天气
变化,再次判断植物生长
package main import "fmt" type Plant struct { Name string Weather *Weather } type Weather struct { Temperature int64 Raining bool } func (plant *Plant) Grow() (result string) { if plant.Weather.Raining == true && plant.Weather.Temperature > 18 { return plant.Name + "生长了" } return plant.Name + "没有生长" } func main() { weather := &Weather{} plant := Plant{ Name: "苹果", Weather: weather, } fmt.Println("======= 第一天 ========") weather.Temperature = 20 weather.Raining = true fmt.Println(plant.Grow()) fmt.Println("======= 第二天 ========") weather.Raining = false fmt.Println(plant.Grow()) }
输出
======= 第一天 ======== 苹果生长了 ======= 第二天 ======== 苹果没有生长
4.3 类图
5. 聚合关系(Aggregation)
5.1 概念
一种强关联关系,也是一种弱。
表现为A包含B,但B可以不在A创建时创建。
5.2 代码示例
- 定义
一堆水果
,水果切片
是它的成员 - 实例化
一堆水果
属于关羽 - 实例化
苹果
和橘子
,将其加入关羽的一堆水果
package main type Fruit struct { Name string } type PileOfFruits struct { Belong string Kinds []*Fruit } func main() { //定义一堆水果 fruitsByGuanYu := PileOfFruits{ Belong: "guanYu", } //定义苹果 apple := &Fruit{ Name: "苹果", } //定义橘子 orange := &Fruit{ Name: "橘子", } //将两种水果加入水果堆 fruitsByGuanYu.Kinds = append(fruitsByGuanYu.Kinds, apple, orange) }
空心菱形+实线
雁群是由一堆大雁组成的
type Goose struct { Bird } type Geese struct { geese []*Goose }
5.3 类图
6. 组合关系(Composition)
6.1 概念
比聚合关系强的关联关系,是强“拥有”
表现为:B是A的组成部分,且A和B有同样的生命周期。
6.2 代码示例
- 定义一个结构体
人
,包含成员名字
、头部
、身体
- 定义结构体
头部
、身体
- 实例化
张飞
的同时创建头
和身体
实例
package main import "fmt" type People struct { Name string Head Head Body Body } type Head struct { Shape string Eyes string Nose string Mouth string } type Body struct { Height int64 Weight int64 } func main() { zhangFei := People{ Name: "张飞", Head: Head{ Shape: "豹头", Eyes: "环眼", Nose: "狮子鼻", Mouth: "大海口", }, Body: Body{ Height: 185, Weight: 200, }, } fmt.Printf("%+v", zhangFei) }
6.3 类图
分类:
go语言开发 / go语言架构
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-07-06 docker-compose启动redis集群