Home > Java > javaTutorial > body text

Detailed explanation of the differences between dependencies, associations, aggregations, and combinations in Java

黄舟
Release: 2017-08-10 13:48:47
Original
1461 people have browsed it

This article mainly introduces the understanding of the difference between Java dependency-association-aggregation-combination. Dependency is easier to distinguish. It is the weakest coupling. The following is a very detailed introduction for those who are interested. Let’s take a look together

First take a look at the definitions of these four relationships in the book:

  • Dependency relationship is the connection between classes. A dependency relationship indicates that one class depends on the definition of another class. For example, a person (Person) can buy a car (car) and a house (House). The Person class depends on the definition of the Car class and the House class, because the Person class references Car and House. Different from association, there are no Car and House type attributes in the Person class. The instances of Car and House are passed into the buy() method as parameters. Generally speaking, dependencies are reflected in the Java language as local variables, formal parameters of methods, or calls to static methods.

  •  Association (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.

  •  Aggregation relationship is a type of association relationship and is a strong association relationship. Aggregation is the relationship between a whole and an individual. For example, the relationship between the automobile category and the engine category, tire category, and other parts categories is the relationship between the whole and the individual. Like association relationships, aggregation relationships are also implemented through instance variables. However, the two classes involved in the association relationship are on the same level, while in the aggregation relationship, the two classes are on unequal levels, one representing the whole and the other representing the part.

  •  Composition relationship is a type of association relationship and is a stronger relationship than aggregation relationship. It requires that the object representing the whole in an ordinary aggregation relationship is responsible for representing the life cycle of part of the object, and the combination relationship cannot be shared. The object representing the whole needs to be responsible for keeping the part object alive and in some cases annihilating the object responsible for the part. An object representing a whole can pass an object representing a part to another object, which is responsible for the life cycle of this object. In other words, the object representing the part can only be combined with one object at each moment, and the latter is exclusively responsible for the life cycle. Parts have the same life cycle as wholes.

——Excerpted from "Java Object-Oriented Programming"

The coupling degree of the above relationships is gradually enhanced (the concept of coupling degree will be discussed in detail later, and it can be understood temporarily here It is the degree of impact on other classes when a class changes. The smaller the impact, the weaker the coupling. The greater the impact, the stronger the coupling.) We already know from the definition that dependence is actually a relatively weak association, aggregation is a relatively strong association, and combination is a stronger association. So if we distinguish them in general, in fact, these four Relationships are all related relationships.

This is a kind of weakest coupling. It is the weakest coupling. It is manifested as a local variable, method parameter in Java, or the call of static methods, as follows as the example below. : The Driver class depends on the Car class, and the three methods of Driver demonstrate three different forms of dependency.


class Car { 
  public static void run(){ 
    System.out.println("汽车在奔跑"); 
  } 
} 
class Driver { 
  //使用形参方式发生依赖关系 
  public void drive1(Car car){ 
    car.run(); 
  } 
  //使用局部变量发生依赖关系 
  public void drive2(){ 
    Car car = new Car(); 
    car.run(); 
  } 
  //使用静态变量发生依赖关系 
  public void drive3(){ 
    Car.run(); 
  } 
}
Copy after login

Association relationships are generally implemented in Java using member variables, and sometimes in the form of method parameters. Still using the Driver and Car examples, the method parameter form can be used to express dependencies or associations. After all, we cannot express semantics too accurately in the program. In this example, member variables are used to express this meaning: the car is my own car, and I "own" this car. Use method parameters to express: The car is not mine, I am just a driver. I drive whatever car others give me, and I use this car.


class Driver { 
  //使用成员变量形式实现关联 
  Car mycar; 
  public void drive(){ 
    mycar.run(); 
  } 
  ... 
  //使用方法参数形式实现关联 
  public void drive(Car car){ 
    car.run(); 
  } 
}
Copy after login

The aggregation relationship is a relatively strong association relationship, which is generally implemented in the form of member variables in Java. There is a relationship between whole and part between objects. For example, in the above example


class Driver { 
  //使用成员变量形式实现聚合关系 
  Car mycar; 
  public void drive(){ 
    mycar.run(); 
  } 
}
Copy after login

If the following semantics are given to the above code: the car is a private car and is part of the driver's property. Then the same code represents an aggregation relationship. Aggregation relationships generally use the setter method to assign values ​​to member variables.

If the following semantics are given: a car is a must-have property for a driver. If you want to be a driver, you must first have a car. If the car is gone, the driver will not want to live. And if the driver stops being a driver, the car will be destroyed and no one else will be able to use it. That means a combination relationship. Generally speaking, in order to express a combination relationship, a constructor is often used to achieve the purpose of initialization. For example, in the above example, a constructor with Car as a parameter is added


public Driver(Car car){ 
  mycar = car; 
}
Copy after login

        所以,关联、聚合、组合只能配合语义,结合上下文才能够判断出来,而只给出一段代码让我们判断是关联,聚合,还是组合关系,则是无法判断的。

The above is the detailed content of Detailed explanation of the differences between dependencies, associations, aggregations, and combinations in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!