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 类图示例

上边的代码创建类图如下:

Pland
+String Leaves
+String Trunk
+String Root
+Grow()
Weather
+Int64 Temperature
+bool Raining

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 类图

Pland
+String Leaves
+String Trunk
+String Root
+Grow()
Fruit
+String Name

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 类图

Pland
+String Name
+Grow()
«interface»
Change
+Crow()

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 类图

Pland
+String Name
+Weather Weather
+Grow()
Weather
int64 Temperature
bool Raining

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 类图

PileFruit
+String Belong
Fruit
+String Name

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 类图

People
+String Name
Head
+String Shape
+String Eyes
+String Nose
+String Mouth
Body
+Int64 Hight
+Int64 Weight

posted on   运维开发玄德公  阅读(209)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-07-06 docker-compose启动redis集群
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示