java--Understanding the Difference Between Dependency, Association, Aggregation and Composition

When learning object-oriented design of object relationships, it is easy to confuse the differences between the four relationships of dependency, association, aggregation, and composition. In particular, the latter three are only different in terms of semantics. The so-called semantics refers to context, specific situations, and so on.

Dependency is the connection between classes. Dependency means 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 definitions of the Car class and the House class, because the Person class references Car and House. Unlike the association, there are no attributes of type Car and House in the Person class. The instances of Car and House are passed to the buy() method as parameters. Generally speaking, dependencies are embodied in the Java language as local variables, formal parameters of methods, or calls to static methods.

Association (Association) relationship is the connection between classes, it allows one class to know the properties and methods of another class. Associations can be bidirectional or unidirectional. In the Java language, associations are generally implemented using member variables.

Aggregation (Aggregation) relationship is a kind of association relationship, is a strong association relationship. Aggregation is the relationship between the whole and the individual. For example, the relationship between the car class and the engine class, tire class, and other parts class is the relationship between the whole and the individual. Like associations, aggregations are implemented through instance variables. However, the two classes involved in the association relationship are at the same level, while in the aggregation relationship, the two classes are at the unequal level, one representing the whole and the other representing the part.

The composition relationship is a kind of association relationship, which is stronger than the aggregation relationship. It requires that the object representing the whole in the common aggregation relationship is responsible for representing the life cycle of some objects, and the composition relationship cannot be shared. Objects that represent the whole need to be responsible for keeping the parts alive and, in some cases, annihilating the parts. An object that represents a whole can pass an object that represents a part to another object, which is responsible for the object's life cycle. In other words, the object representing the part can only be combined with one object at a time, and the latter is exclusively responsible for the life cycle. Parts and wholes have the same life cycle .

The coupling degree of the above relationship increases in turn (the concept of coupling degree will be discussed in detail later. It can be temporarily understood as the degree of impact on other classes when a class changes. The smaller the impact, the weaker the coupling degree and the greater the impact. The larger the coupling, the stronger). We already know from the definition that the dependency relationship is actually a relatively weak association, the aggregation is a relatively strong association, and the combination is a stronger association, so in general terms, in fact, these four Relationships are related relationships.

Dependencies are easy to distinguish. It is the weakest type of coupling. In java, it is expressed as local variables, formal parameters of methods, or calls to static methods, such as the following example: Driver class depends on Car class, Driver The three methods of respectively demonstrate three different forms of dependencies.

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

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

 class Driver {   
        //使用成员变量形式实现关联   
        Car mycar;   
        public void drive(){   
            mycar.run();   
        }   

    //使用方法参数形式实现关联   
        public void drive(Car car){   
            car.run();   
        }   
    }  

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

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

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

If the following semantics are given: a car is a must-have property of a driver. To become a driver, you must first have a car. If the car is gone, the driver does not want to live. And if the driver quits being a driver, the car will be smashed, and no one else will want to use it. That means a combination relationship. Generally speaking, in order to express the combination relationship, the 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;   
    }  

Therefore, association, aggregation, and combination can only be judged by combining semantics and context, but it is impossible to judge whether it is an association, aggregation, or combination relationship only if a piece of code is given.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325471934&siteId=291194637