目录
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 代码示例
植物天气植物生长天气天气天气
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
}