Differences between combination, association, and aggregation

Source: Internet
Author: User

 

Inter-class relationship

In the class diagram, apart from describing the names, attributes, and operations of individual classes, we also need to describe the relationships between classes because no classes exist independently, they usually need to work with other classes to create a separateWorkMore semantics. InUMLIn the class diagram, the relationship is represented by a line between the class boxes. Different modifiers between the line and the line end indicate different relationships. The relationships between classes include inheritance (generalization), Association, aggregation, and combination.

(1) Inheritance: A class (called a subclass) inherits the functions of another class (called a base class) and adds its own new functions, inheritance is the most common relationship between classes. The inherited representation in the class diagram.MethodIs a solid line that pulls a closed, single-key header (or triangle) from the subclass to the base class. For example, in Figure 3.2
The inheritance relationship between the cobject class and the menu class cmenu.

Figure 3.2 class inheritance

Class inheritance is displayed in C ++ as follows:

Class B {}

Class A: Public B {}

(2) Association: A Semantic Link between model elements and a weak link between classes. The association can be unidirectional or bidirectional. You can add an association name to the Association to describe the role of the Association. Classes at both ends of the association can also be associated with a role. A role can have multiple roles, indicating how many classes can be associated.ObjectJoin. You can use the association class to further describe the associated attributes, operations, and other information. The join class uses a dotted line to connect to the join class. Some constraints can be added to the Association to enhance the meaning of the association.

 

The Association is displayed in C ++ as follows:

Class {...}

Class B {...}

A: function1 (B & B) // or a: function1 (B) // or a: function1 (B * B)

That is, a class acts as a parameter of another class method.

(3) aggregation: refers to the relationship between the whole and the part. After defining a whole class, we usually analyze the composition structure of the whole class. In this way, an aggregation relationship is formed between the overall class and the composition class. For example, an aircraft carrier Formation includes a sea-air ing ship, an escort ship, a carrier-based aircraft, and a nuclear-powered attack submarine. The requirement descriptions include "include", "Composition", and "... Partial "and other words often mean the aggregation relationship.

(4) combination: it also indicates the relationship between the whole and the part of the class, but the partial and the whole of the combination have a unified survival period. If the overall object does not exist, some objects do not exist. Some objects have a symbiotic relationship with the entire object.

The difference between aggregation and combination is that the aggregation relationship is "has-a", and the combination relationship is "contains-a". The aggregation relationship indicates that the relationship between the whole and the part is relatively weak, the combination is relatively strong; the Objects representing some things in the aggregation relationship are irrelevant to the lifetime of the objects representing the aggregation things. Once an aggregation object is deleted, the Objects representing some things are not necessarily deleted. Once a composite object is deleted in the composite, the object representing some things is also deleted.

Let's use simple examples to illustrate the differences between clustering and combination. "The country is broken," the country is destroyed, and the family is lost. "The country" and "home" are clearly a combination. On the contrary, the computer and its peripherals are in an aggregation relationship, because the relationship between them is relatively loose, the computer is gone, the peripherals can also exist independently, and can be connected to other computers. In the aggregation relationship, some of them can exist independently of aggregation, and some ownership can be shared by several aggregates. For example, printers can be shared by colleagues in the office.

In C ++, aggregation can be expressed:

Class {...}

Class B {A * ;.....}

Class B contains the pointer of Class;

The combination can be expressed:

Class {...}

Class B {;...}

Class B contains objects of Class.

Accurate UML class diagrams distinguish aggregation and combination with hollow and solid diamond.

Figure 3.4 aggregation and combination

 

Aggregation, association, and combination are three relationships between objects. In a sense, inheritance is a type of vertical relationship, while aggregation, association, and combination are the horizontal relationships of objects.



The link strength is associated with <aggregation <combination

 

The difference between Association and aggregation is mainly in semantics. The two objects associated are generally equal. For example, if you are a friend of mine, aggregation is generally not equal, for example, if a company contains many employees, the implementation is similar. The difference between aggregation and combination is different in semantics and implementation. The life cycle of the two objects in a combination is greatly correlated, the composite object is created when or after the composite object is created and destroyed before the composite object is destroyed. In general, a composite object cannot exist independently from a composite object, and can only belong to one composite object. For example, the version of a document must depend on the existence of the document and can only belong to one document. Aggregation is different. The aggregated object can belong to multiple aggregation objects. For example, an employee may belong to multiple companies.

I would like to give a general example.

The relationship between you and your heart is composition (the heart only belongs to you)

The relationship between you and the book you bought is aggregation (the book may belong to someone else)

There is a association relationship between you and your friends.

The difference between aggregation and combination: Aggregation: scattered aggregation to combination: The whole composed of several independent parts is visible. Aggregation is like placing different fruits in a glass fruit tray, the broken glass fruit disk does not affect the fruit.

[Dependency] [Association] [aggregation] [combination] difference 1. instantiation (dependency)
A uses B as a local variable.
Procedure 1
Void A: Foo ()
{
B;
B. somemethod ();
}

2. Join
There is a certain relationship between A and B. Here we only consider one-way navigation. There are multiple forms of association in the code.
First, as a parameter:
Procedure 2
Void A: Foo (B & B) // (B * B) or (B)
{
B. somemethod ();
}

Second, as a member variable:
Procedure 3
Class
{
Public:
A (B & B)
{
B _ = B;
}

Void Foo ()
{
B _. somemethod ();
}
PRIVATE:
B & B _; // B * B _
};

3. Aggregation
Aggregation is a special association. Aggregation clearly indicates that the aggregate entity has a whole-part relationship. For the code manifestation, see Program 3.

4. Combination
A combination is a special aggregation. a subject in a combination controls the lifecycle of another subject, and they still have a whole-part relationship.
Procedure 4
Class
{
Public:
A ()
{
B _ = new B;
}

~ A ()
{
Delete B _;
B _ = NULL;
}

Void Foo ()
{
B _. somemethod ();
}
PRIVATE:
B _;
};

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.