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

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

Aug 10, 2017 pm 01:48 PM
java combination polymerization

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles