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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

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

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

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.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles