java--understanding of differences between dependencies, associations, aggregations, and combinations

Source: Internet
Author: User

When learning object-oriented design object relationships, the differences between dependencies, associations, aggregations, and combinations of these four relationships are more confusing. In particular, the last three kinds, only in the semantic difference, the so-called semantics refers to the context, specific scenarios and so on.


dependency (Dependency)A relationship is a join between a class and a class. A dependency represents a class that relies on the definition of another class. For example, a person can buy a car and a house, and the People class relies on the definition of the car class and the houses class, because it refers to car and home. Unlike associations, the person class does not have properties for car and house types, and car and house instances are passed to the Buy () method in a parametric way. In general, dependencies are represented in the Java language as local variables, method parameters, or calls to static methods.

Association (Association)A relationship is a join between a class and a class that enables a class to know the properties and methods of another class. Associations can be bidirectional or unidirectional. In the Java language, association relationships are typically implemented using member variables.

Aggregation (Aggregation)Relationship is a kind of relation, and it is a strong association relation. Aggregation is the relationship between the whole and the individual. For example, the relationship between a car and an engine, a tyre, and other parts of a class is the relationship between the whole and the individual. As with associative relationships, aggregation relationships are also implemented through instance variables. However, the two classes involved in the association relationship are at the same level, while in the aggregation relationship, two classes are at the unequal level, one represents the whole and the other represents the part.

combination (composition)Relationship is a kind of relation, which is stronger than aggregation relationship. It requires that an object representing the whole in an ordinary aggregation relationship is responsible for representing the life cycle of a part of the object, and the combined relationship cannot be shared. The object representing the whole needs to be responsible for maintaining part of the object and surviving, and in some cases, the object responsible for representing the part is annihilated. An object representing a whole can pass an object representing a part to another object, which is responsible for the life cycle of the object. In other words, the object representing a part can only be combined with one object at a time, and the latter is exclusively responsible for the life cycle. Part is the same as the whole life cycle.



The coupling degree of the above relationship is enhanced in turn (the concept of coupling degree will be discussed later, here can be temporarily understood as when a class changes, the impact on other classes, the smaller the impact, the weaker the coupling, the greater the impact of the stronger coupling). By definition we already know that the dependency relationship is actually a weaker association, the aggregation is a relatively strong association, and the combination is a stronger association, so the general to distinguish, actually these four kinds of relations, are related relations.

Dependency is a good distinction, it is the weakest coupling, in Java as local variables, method parameters, or calls to static methods, such as the following example: Driver class depends on the car class, driver three methods to demonstrate the dependency of the three different forms.

  1. Class Car {
  2. public static void Run () {
  3. System.out.println ("The car in the Run");
  4. }
  5. }
  6. Class Driver {
  7. //Using formal parameters to have dependencies
  8. public void drive1 (car car) {
  9. Car.run ();
  10. }
  11. //using local variables to have dependency relationships
  12. public void Drive2 () {
  13. Car car = new car ();
  14. Car.run ();
  15. }
  16. //Using static variables to have dependencies
  17. public void Drive3 () {
  18. Car.run ();
  19. }
  20. }




Association relationships are typically implemented in Java using member variables, sometimes in the form of method parameters. Still using driver and car examples, using the method parameter form can represent a dependency or an association relationship, after all, we cannot express semantics too accurately in the program. In this example, the member variable is used to express the meaning: The car is my own car, I "own" the car. Using the method parameter expression: The car is not mine, I am just a driver, others give me what car I will drive what car, I use this car.


  1. Class Driver {
  2. //Use member variable form to implement association
  3. Car Mycar;
  4. public Void Drive () {
  5. Mycar.run ();
  6. }
  7. ...
  8. //Use method Parameter form to implement association
  9. public void Drive (car car) {
  10. Car.run ();
  11. }
  12. }



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

    1. Class Driver {
    2. //Use member variable form to implement aggregation relationship
    3. Car Mycar;
    4. public Void Drive () {
    5. Mycar.run ();
    6. }
    7. }




If the above code is given the following semantics: The car is a private car, is part of the driver's property. Then the same code represents the aggregation relationship. Aggregate relationships typically use setter methods to assign values to member variables.

If the following semantics are given: The car is the driver must have the property, want to become a driver must first have a car, if the car is not, the driver does not want to live. And if the driver does not do the driver, the car will be smashed, no one else to use. That means a combination of relationships. In general, in order to represent a composite relationship, the constructor is often used to achieve the purpose of initialization, for example, in the previous example, plus a construction method with the car parameter

    1. Public Driver (car car) {
    2. Mycar = car;
    3. }



Therefore, the association, aggregation, combination can only match the semantics, combined up and down Wencai can be judged out, and just give a piece of code let us judge whether the association, aggregation, or combination of relations, it is impossible to judge.

java--understanding of differences between dependencies, associations, aggregations, and combinations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.