(Original) What is the difference between Association, aggregation, and composition? (Oo) (UML) (C/C ++)

Source: Internet
Author: User
Class has three relationships: inheritance, implementation, and association. Inheritance and implementation have direct support in C ++, C #, and Java, so I don't understand them. However, association, aggregation, and composition are not supported in the statement, what is the difference between the three?

First, let's look at Association. The following example is a typical Assoication class divisor.

The Association expression is to add an arrow to the actual dataset. There are two important points: Navigability and multiplicity. navigability is the direction of the arrow pointing to a different class, indicates the correlation between two values associated with association. In this class, data member stores the reference (pointer) of the specified class, And the multiplicity is the above number, comparing your own class with your own class, it indicates the relationship between the peer class and your own class. As shown above, one school can have multiple student, one student can only have one school, while one student can choose one to six course and one course can be selected by one or more student.

The common question of association is, what exactly do arrows mean? Generally, if a many-to-many relationship is directed from one to multiple, because a class can have an array or a vector to store multiple objects, however, this is not true. It can only be said that this is the case. The student of the preceding example is more than one for course, and the course is more than one for student, however, he chose course zookeeper Association, So he chose to fully view the design requirements. In practice, it is suggested that the UML for Java programmer Chinese Version p.3-15 be the same, in addition to the stereotype, Zookeeper's suggestion indicates which association it belongs to. In the future, I will introduce it again.

In C ++, the Association Program contains the following 1 class {
2 Private:
3 B * itsb;
4 };

Let's look at aggregation.

The expression of aggregation is represented by the empty diamond and arrow (the selected layout is in UML format, so there is no arrow), and the empty diamond represents the whole, the arrow indicates the part. If it is expressed in English, it is the relation of has a. The upper limit is a typical many-to-many representation.

If it is represented in C ++, the aggregation program contains the following 1 class node {
2 Private:
3 vector <node *> itsnodes;
4 };

The last is composition. The difference between the expression and aggregation is becoming diamond. The other is exactly the same. The difference between the expression and aggregation is in composition when "same Life and Death 』, when the system object dies, the component object will also die, but aggregation is "Life and Death". When the system object dies, the component object is not handled specially.

In C ++, the composition program contains the following 1 class car {
2 public:
3 Virtual ~ Car () {Delete itscarb ;}
4 private:
5 carburetor * itscarb
6 };

The above Code shows that the composition must be handled by the destructor, while aggregation does not.

In C # and Java, there is a garbage collection statement. composition is almost useless, but in C ++, As long as pointer is used, you have to delete it yourself, therefore, composition is very important for C ++.

Reference
Http://www.visualcase.com/kbase/associations.htm
Http://ootips.org/uml-hasa.html
Http://www.tomjewett.com/dbdesign/dbdesign.php? Page = aggregate. php
UML for Java programmer Chinese Version p.3-11 ~ P.3-13
UML Distilled 2nd Chinese Version

Related Article

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.