Differences between combination, aggregation, association, and dependency

Source: Internet
Author: User

Differences between combination, aggregation, association, and dependency

 

 

Dependency and association

Dependency ):

Meaning: it refers to the connection between a class and a class, indicating that a class depends on the definition of another class. Dependency only describes a usage and usage relationship between the class;

Embodiment: Java and C ++ reflect local variables, methods/function parameters, or calls to static methods;

For example, people depend on water and air, and cars depend on gasoline;


The dependency in a UML diagram:


Dependency:

For example, if I want to cross the river without a bridge, I will borrow a boat to cross the river. The relationship between me and the boat is just the relationship between use (borrow.

In code, a method of the dependent class takes the dependent class as its parameter. Or A method of class A creates class B's

Call an instance or a static method of class B. If A depends on B, it means that the change of B may require that A also change;

 



Boat:


[Java]
Static class Boat {
Public static void row (){
System. out. println ("START ");
}
}

Static class Boat {
Public static void row (){
System. out. println ("START ");
}
} Person:


[Java]
Class Person {
Public void crossRiver (Boat boat ){
Boat. row ();
}

Public void fishing (){
Boat boat = new Boat ();
Boat. row ();

Class Person {
Public void crossRiver (Boat boat ){
Boat. row ();
}

Public void fishing (){
Boat boat = new Boat ();
Boat. row (); [java] view plaincopyprint? }

Public void patrol (){
Boat. row ();
}
}

}

Public void patrol (){
Boat. row ();
}
}


Association ):


Meaning: links between classes. Associations enable a class to know the attributes and methods of another class. They generally contain the meaning of "know" and "understand. The association can be bidirectional or unidirectional.

Embodiment: in Java and C ++, associations are implemented through member variables;

For example, fishermen must know the weather conditions before they can go abroad.

Associations represented by UML diagrams:


Association:

A famous example: the relationship between customers and orders and between companies and employees is a link. There is also an example of my bike and I, they both

Is a "ownership" relationship. It is represented in code, that is, an instance of a class containing another class, usually in the form of a class attribute.

The class that appears in the associated class definition can also be expressed as the associated class references a global variable of the type of the associated class. Association can make one-way,

It can also be bidirectional.

 
 


Computer:


[Java]
Class Computer {
Public void develop (){
System. out. println ("Develop ");
}
}

Class Computer {
Public void develop (){
System. out. println ("Develop ");
}
}
 


Person:


[Java]
Class Person {
Private Computer computer;

Public Person (Computer computer ){
This. computer = computer;
}

Public void work (){
Computer. develop ();
System. out. println ("work ");
}

}

Class Person {
Private Computer computer;

Public Person (Computer computer ){
This. computer = computer;
}

Public void work (){
Computer. develop ();
System. out. println ("work ");
}

}

 

 


The difference between dependency and association is that dependency is used and association is owned.
Bytes -------------------------------------------------------------------------------------------------------------

Aggregation ):

Meaning: it is a type of association, a type of strong association (has-a); An aggregation relationship is the relationship between the whole and the individual/part; two links

Classes are at the same level, while the two classes of the aggregation relationship are at different levels. One is the whole, and the other is the individual/part. In the aggregation relationship,

Objects representing the individual/part may be shared by multiple objects representing the whole;

Embodiment: In C ++, the aggregation relationship is implemented by using the aggregate's (array) pointer as an internal Member;

Figure: hollow diamond + solid line + arrow; arrow points to aggregated class, that is, arrow points to individual/part;

For example, when a class or student is deleted, the student still exists and may be referenced by a training institution.

The aggregation relationship represented by a UML diagram:

 

 
 

Classes:


[Java]
Class Classes {
Private Student student;
Public Classes (Student student ){
This. student = student;
}
}

Class Classes {
Private Student student;
Public Classes (Student student ){
This. student = student;
}
}
Student:


[Java]
Class Student {

}

Class Student {

}

 

 


Composition ):

Meaning: it is also a type of association (is-a), but it is a stronger relationship than the aggregation relationship. The composite relationship requires that the aggregate relationship represents the entire object.

It is responsible for the entire life cycle of objects that represent individuals/parts. Composite relationships cannot be shared. In composite relationships, if an object that represents the whole is destroyed or

Destruction, the objects that represent the individual/part of the object will also be destroyed or damaged, and the objects that represent the individual/part of the object may be divided into multiple

Objects that represent the whole are shared, but may not be destroyed or destroyed as a representative object is destroyed or damaged;

Embodiment: in Java and C ++, the composite relationship is implemented through member variables;

Figure: solid diamond + solid line + arrow; arrow points to the object representing the individual/part, that is, the object of the merged/combined class;

For example, a person consists of various organs, such as the head, limbs, and so on, because a person and these organs share the same life cycle, the person is dead, and these organs are also Hung

The relationship between the house and the room. When the house does not exist, the room cannot exist independently.

The combination of UML diagrams:

 
 


Room:


[Java]
Class Room {
Public Room creatoom (){
System. out. println ("Create a room ");
Return new Room ();
}
}

Class Room {
Public Room creatoom (){
System. out. println ("Create a room ");
Return new Room ();
}
}

 

House:


[Java]
Class House {
Private Room room;
Public House (){
Room = new Room ();
}

Public void createHouse (){
Room. creatoom ();

}

}

Class House {
Private Room room;
Public House (){
Room = new Room ();
}

Public void createHouse (){
Room. creatoom ();

}

}
Difference between aggregation and combination: Aggregation means that an individual leaves the whole and can still exist;

The combination is that the individual and the whole cannot be separated, and the individual cannot leave the whole to exist independently.

 

 

 

Differences between dependency, association and aggregation and combination:

Dependency and Association: The relationships between classes are at the same level;

Aggregation and combination: the relationship between classes is expressed as the whole and part.

 

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.