There are a lot of discussions about this on the internet, and I find that there are different opinions on this issue, and they are far from each other. By browsing these discussions and referring to o'reilly-UML 2.0 In A Nutshell (2007), I would like to express my views.
There are many types of Inter-class relationships. There are two types of relationships: vertical and horizontal.
A vertical relationship is an inheritance relationship. Its concept is very clear and it has become one of the three important features of OO. This is not discussed here.
The horizontal relationship is more subtle. According to the UML suggestions, there are roughly four types:
- Dependency)
- Association)
- Aggregation)
- Composition)
There is no objection to their strong/weak relationship: dependency <Association <aggregation <combination
However, the differences between the four of them are not so good, so you need to understand them well.
- Dependency:
- Association:
- UML representation: solid line + arrow
- Link: "... has ..."
- The so-called association means that an object will hold the reference of another object for a long time, and the association between the two is often mutual. The two objects associated with each other do not have any mandatory constraints. As long as the two agree, they can be removed or associated at any time. They have no agreement on the life cycle issue. The associated object can be associated with another object, so the association can be shared.
- There are many typical examples, such:
Class Human
{
ArrayList friends = new ArrayList ();
Public void makeFriend (Human human)
{
Friends. add (human );
}
Public static void main ()
{
Human me = new Human ();
While (true)
{
Me. makeFriend (mySchool. getStudent ());
}
}
}
- Meaning: People keep making friends from birth to death. However, there is no reason to think that the life and death of friends are related to my life and death, so their life periods are not related, my friends can also be friends of others, so they can share.
- Aggregation:
- UML representation: hollow diamond + solid line + arrow
- Link: "... owns ..."
- Aggregation is the association of strong versions. It implies a relationship and a life-cycle relationship. The aggregated objects can be associated with other objects, so the aggregated objects can be shared. Although shared, aggregation represents a more intimate relationship.
- There are many typical examples, such:
Class Human
{
Home myHome;
Public void goHome ()
{
// On the way home
MyHome. openDoor ();
// Watch TV
}
Public static void main ()
{
Human me = new Human ();
While (true)
{
// Go to school
// Eat
Me. goHome ();
}
}
}
- Explanation: My family and I have a strong relationship with each other. My home can be shared, but there are two types of sharing here. One is the sharing of aggregation, which is as strongly associated with the family as you and your wife, and the other is the sharing of aggregation and association, if your friend comes to the house for a meal, you probably won't give him a key.
- Combination:
- UML representation: solid diamond + solid line + arrow
- Link: "... is a part ..."
- A combination is the strongest version of a link. It directly requires that the contained object possess the contained object and the relationship between the contained object and the lifecycle of the contained object. The contained object can be associated with another object, so the contained object can be shared. However, there is no sharing between two contained objects and the same contained object.
- There are many typical examples, such:
Class Human
{
Heart myHeart = new Heart ();
Public static void main ()
{
Human me = new Human ();
While (true)
{
MyHeart. beat ();
}
}
}
- Meaning: A composite relationship is the relationship between the whole and the part. The part belongs to the whole, but does not exist. However, the part does not exist, more specifically, some of them must be created after the overall creation and destroyed before the overall destruction. Some objects can be associated or even aggregated by other objects in this life cycle. However, you must note that once some objects are destroyed as a whole, then the reference in the associated object will become a null reference, which can be guaranteed by the program. The life cycle of a heart is the same as that of a person. If you change the life cycle of a person, for example, the appendix. Many people get tired of it at some time after the birth and destroy it in advance, but its relationship with humans is irretrievably a combination.
In UML, a special case is to allow the object to be included to be transferred to a new object before it is destroyed. Although this is not natural, but it brings the gospel to patients who need heart transplantation.