Java dependence, the difference between the association, aggregation, and combinations of understanding

Dependent (the Dependency) relations between the classes and the class is coupled. Dependency represents a class depends on the definition of another class. For example, a person (Person) can car (CAR) and the house (House), dependent on the Person class definitions and class House Car class, because the Person class references Car and House. It is associated with a different, not the Person class Car and House types of properties, and examples Car House embodiment is based on parameters passed to the method to buy () a. In general, dependence is reflected in the Java language as local variables, parameters, methods, or by calling the static method. 

Association (Association or) the relationship between classes is coupled with the class, it is known that the properties and methods of a class of another class. Association may be bidirectional, it can be one-way. In the Java language, association general use member variables to achieve. 

Polymerization (Aggregation)  relationship is a relationship, the relationship is strong. The relationship between the polymerization and the entire individual. For example, the relationship between the auto and engine type, tire type, as well as other parts of the class will be the overall and individual relationships. As with the relationship, the relationship between the polymerization is achieved by the instance variables. But the association is involved in two classes at the same level, whereas in the aggregation relationship, the two classes is in the level of inequality, a representative of the whole, and the other represents part. 

Composition (Composition)  relationship is a relationship, the relationship is stronger than the relationship between the polymerization. It requires an ordinary polymerization relationships on behalf of the life cycle of the object responsible for representing part of the object as a whole, the combination relationship is not shared. On behalf of the whole object needs to be responsible for maintaining part of the object and survival, in some cases, will be responsible for objects that represent part of annihilation away. Object represents a whole can be passed to the object that represents the part of another object, the latter is responsible for the life cycle of this object. In other words, objects that represent part of the relations can only occur with a combination of the object at every moment, the latter exclusively responsible for the life cycle. Parts and the whole life cycle of the same. 



Coupling of the above relationships were enhanced (concept of the degree of coupling will be discussed in the future, where you can temporarily understand when changes occur when a class is, to the extent caused by the influence of other classes, the smaller the impact of the weaker coupling, the more influence the more powerful of the coupling). By definition we already know, the dependency is actually a weak association, aggregation is a relatively strong correlation, and the combination is a stronger association, so in general terms to distinguish it, in fact, these four relationships are relationships. 

Dependency better distinction, it is a weakest coupling, the performance of local variables in java, parameters, methods, or by calling the static method, as the following example: Driver class depends on the Car class, Driver the three methods were demonstrated in three different forms of dependency.

 1 import java.util.*;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         Driver dr = new Driver();
 6         Car car = new Car();
 7         dr.driver(car);
 8         
 9     }
10 
11 }
12 class Car{
13     public static void run() {
14         System.out.println("汽车在跑");
15     }
16 }
 . 17  class Driver {
 18 is      public  void Driver (Car CAR) {
 . 19          // use parameter dependencies occur embodiment 
20 is          car.run ();
 21 is      }
 22 is      public  void Driver () {
 23 is          // local variable occurs dependencies 
24          = CAR CAR new new Car ();
 25          car.run ();
 26 is      }
 27      public  void driver1 () {
 28          // static method dependency occurs 
29          car.run ();
 30      }
 31 is }
Dependent manner - see Note

Relationship is generally used in a member variable java be achieved, and sometimes by the method parameter forms. Car and Driver is still using the example of using the method parameters can be expressed in the form of dependence, it may also represent a relationship, after all, we can not be too precise semantics in the program. In this example, we use member variables express the idea: the car is my own car, I "own" the car. Use parameter expression: car not mine, I'm just a driver, someone to give me what kind of car I opened the car, I use this car. 

. 1  class Driver {   
 2      // use the variable associated with the form member implementation    
. 3      Car MyCar;   
 . 4      public  void Drive () {   
 . 5          mycar.run ();   
 . 6      }   
 . 7      ...   
 . 8      // use forms of implementing the associated parameters    
. 9      public  void Drive (Car CAR) {   
 10          car.run ();   
 . 11      }   
 12 is }  
Related

If the above code is given to the following semantics: the car is a private car, the driver is part of the property. I.e., the same code of the aggregation relationship. Aggregation relationship setter methods generally used to assign a member variable. 

If given the following semantics: the driver of the car must be some property, in order to become a driver must first have a car, if not the car, the driver also want to live. If the driver quit and the driver, the car smashed, others who also do not want to use. That represents a combination of relationship. Generally, in order to express the relationship between composition, often using a constructor method to achieve the purpose of initialization, in above example, adding a parameter to the constructor for the Car 

1 public Driver(Car car){   
2     mycar = car;   
3 }  
View Code

So, association, aggregation, combined only with semantics, in context to be able to judge them, but only given a code to let us judge that association, aggregation, or a combination of relationship, it can not be judged.

 

Guess you like

Origin www.cnblogs.com/twuxian/p/11325178.html