Comparison and difference between dependence and association

rely

Understand analysis

  1. Dependencies are represented by dotted lines and arrows in the UML diagram. The class pointed by the arrow indicates that the class is dependent.
  2. Dependence reflects a relationship between use and being used. For example, as in the following example, people can rely on cars when driving, and they do not rely on cars when they do not need to drive.
  3. Dependency is the smallest coupling among the five relationships in UML.
  4. The life cycle is very small. After relying on other classes, the relationship between the two classes ends.

Three forms of expression:

  1. The dependent class is passed to the dependent class as a method parameter
  2. The dependent class is declared and instantiated in the method of the dependent class
  3. The dependent class declares a static public method, and the dependent class is directly called in the method through the class name point method.

UML diagram

Insert image description here

Code

/**
 * @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

Understand analysis

  1. The associated relationship is represented by a solid line plus an arrow. The class pointed by the arrow is the class being associated.
  2. Association reflects a stronger relationship than dependence
  3. Association relationship is a connection between classes, which allows one class to know the properties and methods of another class. Associations can be bidirectional or unidirectional. In the Java language, association relationships are generally implemented using member variables.

UML diagram

Insert image description here

Code

/**
 * @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());
    }
}

The difference between dependency and association

● Comparison from the perspective of coupling:
No attributes will be added to the two classes with dependencies. One of the classes is used as a parameter of the method of another class or the variable of a certain method is
related to the two classes. One of the classes is used as an attribute of the other class. The attribute is a closer coupling relationship and is a more The relationship is held for a long time, so the coupling of association is stronger than the coupling of dependency
. From the perspective of the declaration cycle of the two relationships,
the life cycle of dependency only exists in the current method of calling the dependent class. After the method ends The dependency between the two classes ends.
The associated relationship is created when the class is instantiated and ends when the class is destroyed.

Guess you like

Origin blog.csdn.net/zwb568/article/details/125914239