当前位置:网站首页>Comparison and difference between dependence and Association
Comparison and difference between dependence and Association
2022-07-26 07:22:00 【Zhai Wenbiao】
rely on
Understand and analyze
- Dependency in uml Dotted lines and arrows are used in the figure to indicate , The class pointed by the arrow indicates that the class is dependent .
- Dependency embodies a relationship between use and being used , For example, the example in the following example , People can rely on cars while driving , When people don't need to drive, they don't rely on cars .
- Dependency is uml Among the five relationships , The one with the least coupling
- The life cycle is very small , After relying on other classes, the relationship between the two classes ends
Three forms :
- The dependent class is passed to the dependent class as a method parameter
- The dependent class is declared and instantiated in the method of the dependent class
- The dependent class declares a static public method , Dependent classes are called directly in the form of class name point methods in methods .
UML chart

Code implementation
/** * @program: zwbStudy * @description: automobile * @author: Zhaiwenbiao * @create: 2022-07-21 15:04 **/
public class Car {
public void move(){
System.out.println(" The car moves ");
}
public static void speedUp(){
System.out.println(" The car accelerates ");
}
}
/** * @program: zwbStudy * @description: people * @author: Zhaiwenbiao * @create: 2022-07-21 15:04 **/
public class Person {
// The first form of dependency
private void drive(Car car){
car.move();
}
// The second form of dependency
private void drive(){
Car car = new Car();
car.move();
}
// The third form of dependency
private void drive(String param){
Car.speedUp();
}
}
relation
Understand and analyze
- The relation of association is expressed in the form of solid line and arrow , The class pointed by the arrow is the associated class
- The relationship embodied by association is stronger than dependence
- relation (Association) A relationship is a join between classes , It lets one class know the properties and methods of another class . Association can be two-way , It could be one-way . stay Java In language , Association is generally realized by member variables .
UML chart

Code implementation
/** * @program: zwbStudy * @description: Address * @author: Zhaiwenbiao * @create: 2022-07-21 15:10 **/
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
/** * @program: zwbStudy * @description: * @author: Zhaiwenbiao * @create: 2022-07-21 15:10 **/
public class Customer {
private Address address;
// register
private void register(){
address.setAddress(" Anzi District ");
System.out.println(address.getAddress());
}
}
The difference between dependency and Association
● From the perspective of coupling
Neither of the two classes that have dependencies adds properties . One class is used as a parameter of another class's method or a variable of a method
Two classes that are associated with each other , One class acts as an attribute of the other class , Attributes are a more tightly coupled relationship , It's a longer-term relationship , Therefore, the coupling of association is stronger than that of dependence
● Compare from the perspective of the declaration cycle of the two relationships
The life cycle of a dependency only exists in the current method that calls the dependent class , After the method ends, the dependency between the two classes ends .
The relationship of association is generated when the class is instantiated , The association relationship ends when the class is destroyed
边栏推荐
- Compose text and icon splicing to realize drawableleft or drawableright
- Weekly tip 142: multi parameter constructors and explicit
- anaconda安装教程-手把手教你安装
- Leetcode 1184:公交站间的距离
- Singles cup web WP
- Embedded development: tools -- intelligent watchdog design
- Image annotation software reference
- 中国联通改造 Apache DolphinScheduler 资源中心,实现计费环境跨集群调用与数据脚本一站式访问
- Taishan office lecture: word error about inconsistent values of page margins
- DaemonSet
猜你喜欢

依赖和关联的对比和区别

Learn browser decoding from XSS payload

Leetcode 1184:公交站间的距离

OAuth2.0系列博客教程汇总

Singles cup web WP
![From scratch, we will completely develop an online chess game [Gobang] Based on websocket, and only use dozens of lines of code to complete all the logic.](/img/38/24c2e7cfcf794d09fd4d7b86b53728.png)
From scratch, we will completely develop an online chess game [Gobang] Based on websocket, and only use dozens of lines of code to complete all the logic.

Advanced Mathematics (Seventh Edition) Tongji University General exercises two person solution

In July, glassnode data showed that the open position of eth perpetual futures contract on deribit had just reached a one month high of $237959827.

Qt:列表框、表格、树形控件
![[yiku] wonderful! This library organization NPM script is simply explosive!](/img/84/e118c11f27aaf26e52059cb7ebc27a.png)
[yiku] wonderful! This library organization NPM script is simply explosive!
随机推荐
tensorflow2.x中的量化感知训练以及tflite的x86端测评
Lite actor: lightweight optimization of ark actor concurrency model
7月消息,Glassnode数据显示,Deribit上ETH永续期货合约未平仓头寸刚刚达到一个月高点237,959,827美元。
从XSS Playload 学习浏览器解码
Taishan Office Technology Lecture: page width, height, size and source, and the conversion relationship between inches, centimeters and DPI
Introduction to C language -- summary table of operator priority sorting
HCIP---BGP综合实验
Performance test -- lockust performs seckill business scenario test
配置Flask
PG operation and maintenance -- logical backup and physical backup practice
NFT数字藏品开发:数字藏品助力企业发展
Unity3d asynchronous loading of scenes and progress bar loading
又是一年开源之夏,1.2万项目奖金等你来拿!
Compose Canvas line chart
What are the basics of getting started with spot silver
PostgreSQL sequence create alter nextval Curval numerical interval gap
Redis migrate tool migration error.
Airiot IOT platform enables the container industry to build a welding station information monitoring system
Embedded development: tools -- intelligent watchdog design
ModuleNotFoundError: No module named ‘pip‘解决办法