当前位置:网站首页>依赖和关联的对比和区别
依赖和关联的对比和区别
2022-07-26 07:20:00 【翟文彪】
依赖
理解分析
- 依赖关系在uml图中用虚线加箭头表示,箭头指向的类表示该类被依赖。
- 依赖体现的是一种使用和被使用的关系,例如下面的例子中举例的一样,人可以在开车的时候依赖汽车,当人不需要开车的时候就不依赖汽车。
- 依赖关系是uml五种关系中,耦合度最小的一种
- 生命周期很小,在依赖其他的类之后就结束了两个类之间的关系
三种表现形式:
- 被依赖的类作为方法参数传递给依赖类
- 被依赖的类在依赖类的方法中被声明和实例化
- 被依赖的类声明了一个静态公有的方法,依赖类在方法中直接通过类名点方法的形式调用。
UML图

代码实现
/** * @program: zwbStudy * @description: 汽车 * @author: 翟文彪 * @create: 2022-07-21 15:04 **/
public class Car {
public void move(){
System.out.println("汽车移动");
}
public static void speedUp(){
System.out.println("汽车加速");
}
}
/** * @program: zwbStudy * @description: 人 * @author: 翟文彪 * @create: 2022-07-21 15:04 **/
public class Person {
// 第一种依赖形式
private void drive(Car car){
car.move();
}
// 第二种依赖形式
private void drive(){
Car car = new Car();
car.move();
}
// 第三种依赖形式
private void drive(String param){
Car.speedUp();
}
}
关联
理解分析
- 关联的关系用实线加箭头的形式表示,箭头指向的类时被关联的类
- 关联体现的关系比依赖更强
- 关联(Association)关系是类与类之间的联接,它使一个类知道另一个类的属性和方法。关联可以是双向的,也可以是单向的。在Java语言中,关联关系一般使用成员变量来实现。
UML图

代码实现
/** * @program: zwbStudy * @description: 地址 * @author: 翟文彪 * @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: 翟文彪 * @create: 2022-07-21 15:10 **/
public class Customer {
private Address address;
// 注册
private void register(){
address.setAddress("安次区");
System.out.println(address.getAddress());
}
}
依赖和关联的区别
● 从耦合性的角度对比
发生依赖关系的两个类都不会增加属性。其中的一个类作为另一个类的方法的参数或是某个方法的变量
发生关联关系的两个类,其中一个类作为另一个类的属性,属性是一种更为紧密的耦合关系,是更加长久的持有关系,所以关联的耦合性比依赖的耦合性更强
● 从两种关系各自存在的声明周期角度对比
依赖的生命周期仅仅存在于调用被依赖类的当前的方法中,方法结束后两个类之间的依赖关系也就结束了。
关联的关系是在类被实例化的时候就产生了,当类销毁的时候关联关系才结束
边栏推荐
- C51 and MDK coexist keil5 installation tutorial
- 又是一年开源之夏,1.2万项目奖金等你来拿!
- Upgrade ecological proposition: what has Alibaba cloud brought to thousands of businesses?
- Difference between shape and size ()
- HCIP---BGP综合实验
- Common CMD instructions
- [C language] do you really know printf? (printf is typically error prone, and collection is strongly recommended)
- Leetcode:749. isolate virus [Unicom component + priority queue + status representation]
- NFT digital collection system development: what are the best digital marketing strategies for NFT digital collection
- NFT digital collection system development: activating digital cultural heritage
猜你喜欢

Drools (3): drools basic syntax (1)

redis-migrate-tool迁移报错。

C51 and MDK coexist keil5 installation tutorial

Opencv learning warp Perspective

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.

C51与MDK共存 Keil5安装教程

Become an Apache contributor, so easy!
![Talent column | can't use Apache dolphin scheduler? The most complete introductory tutorial written by the boss in one month [3]](/img/21/c35b1493a36c46a89db0c6ebcf46db.jpg)
Talent column | can't use Apache dolphin scheduler? The most complete introductory tutorial written by the boss in one month [3]

Precious metal knowledge: lethal short-term secret script

Apache dolphin scheduler version 3.0.0-beta-1 was released, and flinksql and Zeppelin task types were added
随机推荐
Compose canvas custom circular progress bar
单身杯web wp
Drools (4): drools basic syntax (2)
NFT digital collection system development: digital collections give new vitality to brands
unity3d-对象池的用法
NFT digital collection system development: how enterprises develop their own digital collection platform
Screen: frame paste, 0 fit, full fit
Talent column | can't use Apache dolphin scheduler? The most complete introductory tutorial written by the boss in one month [3]
Compose text and icon splicing to realize drawableleft or drawableright
每周小贴士#142:多参数构造函数和explicit
[romance understood by technical talents] tidb community has prepared a gift for your partner for the "Tanabata Festival". Reply: if I want to challenge, I can participate in the activity!
Precious metal knowledge: lethal short-term secret script
Event loop in browser
NFT数字藏品系统开发:文学+数字藏品的碰撞
Weekly tip 142: multi parameter constructors and explicit
How to convert multi row data into multi column data in MySQL
QT: list box, table, tree control
Introduction to C language -- summary table of operator priority sorting
Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
How to expand and repartition the C disk?