UML diagram example: (you can use startuml for drawing, small size ^_^)
Aggregation: indicates that two objects are integral and partial.WeakLink, and the lifecycle of some parts can go beyond the whole. For example, a computer or a mouse can be represented in a single shape:
Combination: indicates that two objects are integral and partial.StrongLink. Some lifecycles cannot go beyond the whole, or exist without the whole. The "part" of the composite relationship cannot be shared across the whole. Such as the relationship between people and eyes:
However, if you want to say that the eyes can be transplanted, does it mean that they can be separated from people and they are not composite relationships? In fact, the relationship between objects in UML is defined in the corresponding software environment or actual scenario. The relationship between aggregation and combination is distinguished here, the key lies in the strong and weak relationship between the entire and some of them, as well as the dependency between them. If the eyes can be transplanted to others, you can think of them as aggregation, which should be explained in combination with actual scenarios.
2. Implement realization.
2. Dependency, association, aggregation and combination
1. instantiation (dependency)
A uses B as a local variable.
Void A: Foo ()
B. somemethod ();
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:
Void A: Foo (B & B) // (B * B) or (B)
B. somemethod ();
Second, as a member variable:
A (B & B)
B _ = B;
Void Foo ()
B _. somemethod ();
B & B _; // B * B _
Aggregation is a special association. Aggregation clearly indicates that the aggregate entity has a whole-part relationship. For the code manifestation, see Program 3.
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.
B _ = new B;
~ A ()
Delete B _;
B _ = NULL;
Void Foo ()
B _-> somemethod ();
B * B _;
The relationship between clustering and combination in UML
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.
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 the aggregation, and some ownership can be shared by several aggregates. For example, printers can be shared by colleagues in the office.
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.
UML class relationships: dependency, association, aggregation and combination