0
00:00:02,850 --> 00:00:06,113
前面三个视频我们讲了三点
1
00:00:06,113 --> 00:00:07,620
聚合根的问题
2
00:00:10,120 --> 00:00:12,820
123这三点
3
00:00:13,200 --> 00:00:16,512
接下来,我们再说一个小问题
4
00:00:16,512 --> 00:00:18,500
就是一个用词的问题
5
00:00:20,030 --> 00:00:21,430
你看它这里
6
00:00:22,280 --> 00:00:24,421
说聚合***
7
00:00:24,421 --> 00:00:25,760
他用了一个
8
00:00:26,040 --> 00:00:26,256
9
00:00:26,256 --> 00:00:27,340
associated
10
00:00:28,290 --> 00:00:28,774
11
00:00:28,774 --> 00:00:30,710
相关联的
12
00:00:31,980 --> 00:00:32,980
对象
13
00:00:36,540 --> 00:00:38,462
那我们可以对比一下
14
00:00:38,462 --> 00:00:38,890
你看
15
00:00:39,870 --> 00:00:42,600
这是Vaughn Vernon的
16
00:00:43,410 --> 00:00:45,550
实现领域驱动设计,这个书
17
00:00:46,070 --> 00:00:47,670
你看这里用词
18
00:00:48,160 --> 00:00:49,945
他用的是什么
19
00:00:49,945 --> 00:00:50,540
related
20
00:00:51,680 --> 00:00:53,000
相关的
21
00:00:54,820 --> 00:00:56,310
Vernon也用了
22
00:00:56,610 --> 00:00:58,230
association
23
00:00:58,510 --> 00:00:59,700
关联这个词
24
00:00:59,980 --> 00:01:01,330
但他是用在这里
25
00:01:04,800 --> 00:01:08,020
那我们来看,这两个有什么区别
26
00:01:11,250 --> 00:01:13,130
我们把意思列一下
27
00:01:13,510 --> 00:01:13,774
28
00:01:13,774 --> 00:01:16,153
看起来都差不多的
29
00:01:16,153 --> 00:01:21,440
但实际上在我们的软件开发里面是有区别的
30
00:01:22,910 --> 00:01:26,530
associated,相关联的,association
31
00:01:26,820 --> 00:01:28,080
关联
32
00:01:28,360 --> 00:01:31,420
related,相关的,relation
33
00:01:32,940 --> 00:01:33,890
关系
34
00:01:35,140 --> 00:01:40,032
我们就拿UML里面的用词来说
35
00:01:40,032 --> 00:01:41,320
关系是一个
36
00:01:43,110 --> 00:01:44,800
比较大的词
37
00:01:45,890 --> 00:01:48,580
你静态动态都可以
38
00:01:49,000 --> 00:01:50,670
只要这两个之间
39
00:01:50,950 --> 00:01:53,440
你能说得上某种关系就可以
40
00:01:55,070 --> 00:01:58,140
而关联只是关系的一种
41
00:01:58,670 --> 00:01:59,822
是哪一种
42
00:01:59,822 --> 00:02:01,166
就是静态的关系
43
00:02:01,166 --> 00:02:02,702
而且是实例之间的
44
00:02:02,702 --> 00:02:04,430
实际上就相当于属性
45
00:02:05,270 --> 00:02:05,535
46
00:02:05,535 --> 00:02:06,330
比如说
47
00:02:06,620 --> 00:02:09,260
订单和订单项是关联
48
00:02:10,840 --> 00:02:12,973
订单和顾客是关联
49
00:02:12,973 --> 00:02:14,893
订单和下单日期是关联
50
00:02:14,893 --> 00:02:17,880
或者说,人和他的姓名也是关联
51
00:02:18,510 --> 00:02:18,736
52
00:02:18,736 --> 00:02:19,417
属性嘛
53
00:02:19,417 --> 00:02:22,595
只不过这个属性的类型是一个什么
54
00:02:22,595 --> 00:02:23,730
可能是一个
55
00:02:24,350 --> 00:02:25,080
基本类型
56
00:02:25,080 --> 00:02:27,090
比如字符串什么之类的
57
00:02:28,220 --> 00:02:29,450
这是关联
58
00:02:32,360 --> 00:02:36,161
那为什么这个用词,用这个不恰当
59
00:02:36,161 --> 00:02:37,790
这个是恰当的
60
00:02:38,470 --> 00:02:39,740
因为什么
61
00:02:41,730 --> 00:02:45,610
我们的这种整体部分关系里面
62
00:02:46,460 --> 00:02:47,830
这些部件
63
00:02:48,110 --> 00:02:49,200
和整体
64
00:02:50,050 --> 00:02:51,720
它是存在关联
65
00:02:52,000 --> 00:02:52,222
66
00:02:52,222 --> 00:02:53,110
比如说,人
67
00:02:53,930 --> 00:02:54,408
68
00:02:54,408 --> 00:02:56,800
跟手、眼、心、肝
69
00:02:58,510 --> 00:03:00,630
存在关联
70
00:03:00,920 --> 00:03:02,420
人组合这些
71
00:03:02,960 --> 00:03:04,800
手、眼、心、肝对象
72
00:03:07,060 --> 00:03:09,970
但是呢,这些部件之间
73
00:03:10,410 --> 00:03:11,700
它们之间
74
00:03:13,400 --> 00:03:15,450
是不存在关联的
75
00:03:16,490 --> 00:03:18,780
它们只能通过这个人
76
00:03:19,990 --> 00:03:21,330
相关
77
00:03:21,670 --> 00:03:22,806
它们不存在
78
00:03:22,806 --> 00:03:24,170
也不应该存在
79
00:03:24,850 --> 00:03:26,484
你存在就冗余了
80
00:03:26,484 --> 00:03:27,185
对不对
81
00:03:27,185 --> 00:03:28,820
只能由这个老大
82
00:03:31,200 --> 00:03:36,910
才能够知道,由它知道,由它来调配各种责任
83
00:03:38,590 --> 00:03:39,520
所以呢
84
00:03:39,950 --> 00:03:42,333
如果你说它们之间有关联
85
00:03:42,333 --> 00:03:43,200
意思就是
86
00:03:43,730 --> 00:03:45,196
它们之间
87
00:03:45,196 --> 00:03:47,292
私下还有这样的勾结
88
00:03:47,292 --> 00:03:48,550
没有勾结的
89
00:03:48,830 --> 00:03:50,788
它们私下没有勾结的
90
00:03:50,788 --> 00:03:52,530
只能通过这个老大
91
00:03:53,620 --> 00:03:54,710
来处理
92
00:03:57,760 --> 00:03:57,995
93
00:03:57,995 --> 00:03:58,230
94
00:03:58,230 --> 00:03:59,406
这样是错的
95
00:03:59,406 --> 00:04:04,347
所以呢,不能说它们之间是一簇紧密关联的
96
00:04:04,347 --> 00:04:04,817
不行
97
00:04:04,817 --> 00:04:06,699
你说紧密相关
98
00:04:06,699 --> 00:04:07,170
可以
99
00:04:08,180 --> 00:04:09,870
怎么个相关法呀
100
00:04:11,970 --> 00:04:12,203
101
00:04:12,203 --> 00:04:14,770
比如说,在分配责任的时候
102
00:04:16,290 --> 00:04:17,750
可以由这个
103
00:04:18,140 --> 00:04:18,980
人
104
00:04:19,260 --> 00:04:23,551
来给后面的部件对象分配责任
105
00:04:23,551 --> 00:04:25,570
分解、分配,大的责任进来
106
00:04:25,570 --> 00:04:26,075
对吧
107
00:04:26,075 --> 00:04:28,600
这大的,然后分解成,小的
108
00:04:28,880 --> 00:04:29,780
小的
109
00:04:30,060 --> 00:04:30,330
110
00:04:30,330 --> 00:04:33,300
各司其职、各尽其能嘛
111
00:04:33,300 --> 00:04:33,840
对吧
112
00:04:35,390 --> 00:04:36,581
这可以
113
00:04:36,581 --> 00:04:39,680
由对象为它的部件分配责任
114
00:04:40,220 --> 00:04:40,380
115
00:04:40,380 --> 00:04:41,020
这可以
116
00:04:41,020 --> 00:04:42,780
就是我们前面说的这个嘛
117
00:04:42,780 --> 00:04:43,100
对吧
118
00:04:45,030 --> 00:04:47,574
外面是不能直接访问这个对象的
119
00:04:47,574 --> 00:04:49,140
任何一个部件的
120
00:04:50,150 --> 00:04:52,270
或者说,对象是封装的
121
00:04:56,690 --> 00:04:58,180
那可能有的人说
122
00:04:59,260 --> 00:05:00,580
它们之间
123
00:05:00,860 --> 00:05:02,660
也可以有关系
124
00:05:02,660 --> 00:05:03,660
什么关系
125
00:05:03,660 --> 00:05:04,260
比如说
126
00:05:05,050 --> 00:05:07,460
它们之间有同事关系
127
00:05:08,290 --> 00:05:08,980
128
00:05:10,450 --> 00:05:11,890
有同事关系
129
00:05:15,550 --> 00:05:16,439
没有的
130
00:05:16,439 --> 00:05:19,329
它们之间这个同事关系是什么
131
00:05:19,329 --> 00:05:20,219
是冗余的
132
00:05:20,219 --> 00:05:21,553
是推导出来的
133
00:05:21,553 --> 00:05:22,220
是因为
134
00:05:23,170 --> 00:05:24,920
它们都是什么
135
00:05:25,510 --> 00:05:27,480
跟它形成了一个
136
00:05:28,130 --> 00:05:30,040
整体-部分的关系
137
00:05:30,320 --> 00:05:32,806
所以才会推导出同事关系
138
00:05:32,806 --> 00:05:35,520
就像我们的家里面,兄弟一样
139
00:05:35,880 --> 00:05:38,512
你说我跟你是兄弟关系
140
00:05:38,512 --> 00:05:39,610
那怎么来的
141
00:05:41,320 --> 00:05:42,680
它是从什么
142
00:05:43,000 --> 00:05:44,200
我跟
143
00:05:45,150 --> 00:05:46,640
老爸是父子
144
00:05:46,640 --> 00:05:49,620
你跟老爸也是父子
145
00:05:50,130 --> 00:05:52,296
所以我们是兄弟关系
146
00:05:52,296 --> 00:05:53,741
它是个冗余的
147
00:05:53,741 --> 00:05:53,982
148
00:05:53,982 --> 00:05:56,390
你这边私下再搞一个
149
00:05:57,040 --> 00:05:58,927
那就会冲突了
150
00:05:58,927 --> 00:06:00,604
可能就会有冲突了
151
00:06:00,604 --> 00:06:02,072
我跟你是兄弟
152
00:06:02,072 --> 00:06:03,540
而且是亲兄弟
153
00:06:03,820 --> 00:06:07,350
结果呢我们的父母不相同
154
00:06:07,990 --> 00:06:12,573
刚才我们说父亲,我们是把它简化了
155
00:06:12,573 --> 00:06:17,385
我们就说,我们的上一辈都没有相同的
156
00:06:17,385 --> 00:06:18,760
你说,这兄弟
157
00:06:18,760 --> 00:06:18,990
哪里来
158
00:06:18,990 --> 00:06:21,740
除非你说的是结拜的干兄弟
159
00:06:23,830 --> 00:06:25,410
包括同事也是一样
160
00:06:25,410 --> 00:06:27,187
为什么我们是同事
161
00:06:27,187 --> 00:06:30,150
因为我们是在同一个公司里面工作
162
00:06:30,630 --> 00:06:33,330
为同一个雇主卖命
163
00:06:33,730 --> 00:06:34,550
164
00:06:35,270 --> 00:06:39,960
它是通过员工和公司之间的关系得来的
165
00:06:41,480 --> 00:06:42,850
是一个推导的
166
00:06:46,420 --> 00:06:47,700
所以呢,这个
167
00:06:48,300 --> 00:06:49,460
看起来
168
00:06:49,910 --> 00:06:52,608
好像是一个用词上的随意选择
169
00:06:52,608 --> 00:06:56,551
背后很可能是一个什么,某方面知识的缺失
170
00:06:56,551 --> 00:06:56,759
171
00:06:56,759 --> 00:07:00,910
我们前面讲到糊涂用语的时候也说过这个问题
172
00:07:01,470 --> 00:07:04,450
很多时候我们用词乱用
173
00:07:04,780 --> 00:07:07,021
它不是一个习惯问题
174
00:07:07,021 --> 00:07:09,670
而是我们对某些概念
175
00:07:09,950 --> 00:07:12,220
没有理解清楚的问题
0
00:00:01,740 --> 00:00:04,230
你看,Vernon这里
1
00:00:04,780 --> 00:00:06,487
他这里用的是related
2
00:00:06,487 --> 00:00:06,701
3
00:00:06,701 --> 00:00:08,622
这里就用association
4
00:00:08,622 --> 00:00:09,690
你看,他是说
5
00:00:10,340 --> 00:00:10,735
6
00:00:10,735 --> 00:00:13,500
可以往深处导航
7
00:00:13,780 --> 00:00:17,060
怎么导航,肯定是有关联关系的
8
00:00:17,340 --> 00:00:18,030
对吧
9
00:00:18,790 --> 00:00:21,600
所以呢,你看它这个,相当于一路点下去
10
00:00:21,600 --> 00:00:22,725
a.b.c嘛
11
00:00:22,725 --> 00:00:23,100
对吧
12
00:00:24,000 --> 00:00:26,855
访问它的下属(部件)
13
00:00:26,855 --> 00:00:30,490
所以它这里用了一个关联
14
00:00:31,010 --> 00:00:33,590
这个用词就是比较准确的
15
00:00:33,940 --> 00:00:35,983
你看,这里用related
16
00:00:35,983 --> 00:00:38,480
这里用的是associations
0
00:00:00,210 --> 00:00:01,880
如果有的同学说
1
00:00:02,220 --> 00:00:02,452
2
00:00:02,452 --> 00:00:03,850
一定要这样吗
3
00:00:04,130 --> 00:00:07,138
我就是要大家私下里这样搞
4
00:00:07,138 --> 00:00:07,890
不行吗
5
00:00:08,190 --> 00:00:08,653
6
00:00:08,653 --> 00:00:10,042
当然可以的
7
00:00:10,042 --> 00:00:11,200
你这样搞
8
00:00:11,670 --> 00:00:14,020
照样能够生成代码
9
00:00:14,020 --> 00:00:16,370
照样能够编译通过
10
00:00:16,650 --> 00:00:18,640
只不过结构
11
00:00:19,010 --> 00:00:20,660
不够好而已
12
00:00:20,940 --> 00:00:22,487
没有什么不可以的
13
00:00:22,487 --> 00:00:23,068
对不对
14
00:00:23,068 --> 00:00:24,422
我们说这些,只是说
15
00:00:24,422 --> 00:00:25,002
16
00:00:25,002 --> 00:00:25,970
你这样的话
17
00:00:26,350 --> 00:00:26,625
18
00:00:26,625 --> 00:00:29,106
你软件的结构不够好
19
00:00:29,106 --> 00:00:30,760
应变能力差
20
00:00:31,350 --> 00:00:33,218
并不是说不能够实现
21
00:00:33,218 --> 00:00:34,879
不能够编译通过
22
00:00:34,879 --> 00:00:36,540
你说人是狗的一种
23
00:00:37,150 --> 00:00:38,987
照样可以编译通过
24
00:00:38,987 --> 00:00:39,600
对不对
25
00:00:40,110 --> 00:00:42,360
但是你这样做有什么问题
26
00:00:43,020 --> 00:00:44,056
因为在领域逻辑里面
27
00:00:44,056 --> 00:00:46,130
人不是狗的一种
28
00:00:47,450 --> 00:00:48,965
然后你在代码里面
29
00:00:48,965 --> 00:00:50,480
或者说在你的
30
00:00:51,140 --> 00:00:52,500
类的设计里面
31
00:00:52,500 --> 00:00:55,220
你把它定义成人是狗的一种
32
00:00:56,610 --> 00:00:59,080
不符合领域逻辑
33
00:00:59,930 --> 00:01:01,490
迟早会出问题