当前位置:网站首页>Six relationships of classes -- the difference between dependency and Association
Six relationships of classes -- the difference between dependency and Association
2022-07-27 18:08:00 【Charmaine Xia】
For a lot of new contacts UML Children's shoes , You may not understand the associations and dependencies between classes , Let's talk about the dishes today .
rely on
Expressed as parameters in the function (use a), Is the connection between classes , Indicates that one class depends on the definition of another class , A change in one class will affect the other . For example, if A Depend on B, be B Embodied as local variables , Method parameters 、 Or static method calls . Like TV (TV) Channel dependent (channel) Common dependencies are as follows :
(1) class B Pass in the class as a parameter A Methods . I personally call it “ Parameter dependence ”.
(2) class B Exists in a class as a local variable A The methods of . I personally call it “ Local dependence ”.
(3) class A Calling class B A static property or method of . I personally call it “ Static dependency ”.
UML The implementation in the figure uses a line with The dotted line of the arrow points to the dependent class , as follows :
relation ( delegate )
Expressed as variables (has a), Join between classes , It lets one class know the properties and methods of another class . For example, if A Relating to B, be B Embodied in A Global variable of , Such as person Classes and company class .
There are two-way Association and one-way Association :
1、 Two way connection : Both classes know the common properties and operations of the other class .
2、 One way Association : Only one class knows the public properties and operations of another class .
Most associations should be one-way , One way relationships are easier to establish and maintain , It is helpful to find classes that can be taken .
UML The implementation in the figure uses a solid line ( Some places use A solid line with an arrow ) Connect the same or different classes , as follows :
This is indeed a bit messy , But Xiaocai suddenly found a better entry point , Take it out and share it .
Readers who have been exposed to design patterns , You will often see such scenes : In instantiation A Class time , need B Class as a parameter of the constructor , This explanation A Class needs to hold a B Class reference . For example, the agency model 、 decorate Patterns, etc , They all do that . for example Java Medium IO The flow adopts the decoration mode , So we often see such statements :new BufferInputStream(new FileInputStream("c:\\1.db"));
person And company The relationship between is also called delegation relationship ( What I call myself ).
This holds references , Is a simple relationship . In the code, it's shown as : stay A Class has a member variable , The type of variable is B class ,A Class holds B Class reference , Just explain A Classes and B Class is associated .
use UML The figure is as follows :

A little explanation , Because it is A Class holds B Class reference , Therefore, the correlation is from A Class ( from A Class induced ), So the arrow should start from A Class point B class .
Usually , This simple one-way association is enough , But the relationship is mainly used in database design .
In database design , Whether it's one-on-one 、 One to many 、 Many to many , Are not one-way .
From the perspective of tables , Both of them can determine the other end from either end . Take one to many , With one The primary key of the end , According to many Check the foreign key of the end table many End data ; With many End foreign key , According to one Check the primary key of the end table one End data .
From the perspective of entity classes , You can also determine the other end from either end . Take one to many ,one The end entity class will hold a many End reference collection , for example private Set<B> bs;, Query to one End , You can read directly from this collection many End ;many The end entity class will hold a one End references , for example private A a;, Query to many End , You can identify each directly from this reference many Terminal one End .
thus , It becomes a two-way Correlation , use UML When drawing relationships , Add arrows on both sides , This is too ugly , Simply do not add .
For example, the relationship between department entity class and employee entity class , You can say that :

Because it is the association relationship between database entity classes , Therefore, we need to add the quantitative relationship ,1 representative one End ,0..n representative many End , Explain that a department can have multiple employees , But an employee can only belong to one department , adopt UML The figure describes one to many .
This is the typical application of Association .
I have to mention that , Associations can also be broken down into aggregation and composition ( The specific concepts of the two are searched by the readers themselves ).
Small dish discovery polymerization 、 Combination can be understood from another angle .
polymerization
Let's start with aggregation , It is a weak correlation , The general meaning is that the whole and the part can exist independently . If we change the angle , It can be regarded as a cascading operation of the database .
Take the group and its members for example , When deleting a group , Delete the members of this group , This is obviously unscientific , Because the group and its members are weakly related , The group can have any one member , A team member can also go to any group , This group no longer exists , You can go to another group , They are not necessarily related , It can be called aggregation .
therefore , When we were designing the database , Cascading deletion is often not set , in other words , Deleting a group does not delete group members .
UML The figure is as follows :

Hollow diamond for aggregation , Point to one End .
Combine
Let's talk about combination , Composition is a strong association , The general meaning is that the whole and the part are inseparable , Can't exist independently . Also understand from cascade operation .
Take students and student ID cards for example , If a student drops out , No longer belong to this school , Then consider deleting the student information , When deleting , The student ID information corresponding to the student will also be deleted , You can add cascading deletion . Because the student ID card belongs to a student's proprietary information , The student no longer exists , The student ID card cannot be used by others , So it's a strong correlation , It can be called combination .
UML The figure is as follows :

The last thing to talk about is dependency .
If A Class , Used B class , Then say A Classes depend on B class , They are dependencies .
A Class uses B class , Maybe the parameter of the method is B class , It is also possible to get a B Class instance . But in either case ,B Class in A Classes are all based on local variable In the form of .
therefore ,A There are B Type of local variable , Just say A Classes depend on B class .
UML The figure is as follows :

Dashed arrows indicate dependencies , The arrow points to the dependent class .
Sum up , There is a simple rule of judgment : A class appears in another class as a member variable , The two are related ; One class appears in another class as a local variable , The two are dependencies .
Be careful : This article is for the convenience of explanation , Always take class as an example , This is not a good design thinking . In development , In order to achieve better " open - Closed principle ", Generally, the interface is defined , Interface dependent , Depend on abstraction , Not according to specific programming , I hope readers will not be misled by the side dishes !!
边栏推荐
- golang 等待一组goroutine完成,并带返回值(2)
- Notes on standardized management of "ancestral warehouse" of meituan meal
- Anaconda uninstall again
- 美团到餐“祖传数仓”标准化治理笔记
- I got the P8 "top-level" distributed architecture manual crazy spread on Alibaba intranet
- hutool- 数组工具
- 年终总结模板
- Initial polymorphism
- 面试常见问题一二
- Wechat applet to make calls
猜你喜欢

Convolutional neural network -- Introduction to FPN (feature pyramid networks)

Wechat applet cloud function batch delete multiple data error: errcode: -502005 database collection not exists

vim的配置及基础使用

In the fourth week of July, Yidun business risk control focused on the supreme law to regulate app's forcible request for personal information

卷积神经网络——YOLOV2(YOLO9000)论文翻译

Learn from things | Yidun mobile terminal isomorphism practice, improve the official website interaction experience in a few steps

Telecommuting can be easily realized in only three steps

Kubernetes 1.24 high availability cluster binary deployment

vue使用keep-alive实现页面缓存

WebDriverException( selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executabl
随机推荐
shell常见命令(1)——变量大小写转换
Establishing SSL connection without server‘s identity verification is not recommended
EF框架简介
Mlx90640 infrared thermal imager temperature sensor module development notes (VII)
查找表中多余重复记录并删除保留最小一个
js实现右键菜单栏功能
[user article] examples of P4 consolidation practice guide disassemble resolve
canvas根据坐标点绘制图形
Convolutional neural network -- Translation of yolov2 (yolo9000) papers
Original direct selling MOS tube knl42150 2.8a/1500v applicable photovoltaic inverter can provide samples
如何限制root远程登入,使普通用户拥有root权限
Code compliance: five reasons why developers use helix QAC
Fast analysis combined with Haidian medicine
MySQL 中 You can‘t specify target table ‘表名‘ for update in FROM clause错误解决办法
快解析结合海典医药
How to develop an online Excel spreadsheet system (Part 1)
Zhengzhou University database course resource description
The global cloud market is growing rapidly, and data security has entered a strong regulatory era of rule of law
【云图说】 第249期 移动应用安全服务—App的体检中心,全面检测,安全上路!
js工具-cookie简单封装