当前位置:网站首页>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
边栏推荐
- Apache DolphinScheduler 2.X保姆级源码解析,中国移动工程师揭秘服务调度启动全流程
- This section is for Supplement 2
- IDEA快捷键
- WCF 部署在IIS上
- How to convert multi row data into multi column data in MySQL
- NFT数字藏品系统开发:华为发布首款珍藏版数字藏品
- Uncover the mystery of cloud native data management: operation level
- Examples of financial tasks: real-time and offline approval of three scenarios and five optimizations of Apache dolphin scheduler in Xinwang bank
- 3.0.0 alpha 重磅发布!九大新功能、全新 UI 解锁调度系统新能力
- Taishan Office Technology Lecture: how to calculate page blank (margin)
猜你喜欢

Hands on practice - teach you how to make an intelligent fish tank with STM32

Examples of financial tasks: real-time and offline approval of three scenarios and five optimizations of Apache dolphin scheduler in Xinwang bank

Image annotation software reference

Singles cup web WP

pycharm常用快捷键

Lite actor: lightweight optimization of ark actor concurrency model

anaconda安装教程-手把手教你安装

Relevant configurations of pychart: change font style and size, change picture background, and change the font color of console output

404 page best practices to improve user experience

屏:框贴、0贴合、全贴合
随机推荐
如何对C盘进行扩容重新分区?
Airiot IOT platform enables the container industry to build a welding station information monitoring system
NFT digital collection system development: the collision of literature + Digital Collections
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-5 personal solutions
金融任务实例实时、离线跑批Apache DolphinScheduler在新网银行的三大场景与五大优化
Hcip - MPLS Technology
6. Backup and recovery of MySQL database
Introduction to C language -- summary table of operator priority sorting
Apache DolphinScheduler&TiDB联合Meetup | 聚焦开源生态发展下的应用开发能力
Pycharm的相关配置:改字体样式和大小、更改图片背景、更改控制台输出的字体颜色
Examples of financial tasks: real-time and offline approval of three scenarios and five optimizations of Apache dolphin scheduler in Xinwang bank
Anaconda installation tutorial - hands on installation
QT: list box, table, tree control
Unity3d asynchronous loading of scenes and progress bar loading
Embedded development: tools -- intelligent watchdog design
单身杯web wp
Compose Canvas line chart
Now the metauniverse is no longer an existence that is only applied to the social field
此章节用于补充
中国联通改造 Apache DolphinScheduler 资源中心,实现计费环境跨集群调用与数据脚本一站式访问