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 类图示例
上边的代码创建类图如下:
classDiagram
Pland ..> Weather
class Pland{
+String Leaves
+String Trunk
+String Root
+Grow()
}
class 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 类图
classDiagram
Pland <|-- Fruit
class Pland{
+String Leaves
+String Trunk
+String Root
+Grow()
}
class 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 类图
classDiagram
Pland ..|> Change
class Pland{
+String Name
+Grow()
}
class Change{
<<interface>>
+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 类图
classDiagram
Pland --> Weather
class Pland{
+String Name
+Weather Weather
+Grow()
}
class 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 类图
classDiagram
PileFruit o.. Fruit
class Fruit{
+String Name
}
class PileFruit{
+String Belong
}
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 类图
classDiagram
People *.. Head
People *.. Body
class People{
+String Name
}
class Head{
+String Shape
+String Eyes
+String Nose
+String Mouth
}
class Body{
+Int64 Hight
+Int64 Weight
}

浙公网安备 33010602011771号