UML class relationships: dependency, association, aggregation and combination

Source: Internet
Author: User

UML diagram example: (you can use startuml for drawing, small size ^_^)

Http://www.blogjava.net/lukangping/archive/2010/08/01/327693.html

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.

Http://hi.baidu.com/szbdyy/item/57c51a3ab94340c51b9696a7

Generalization

2. Implement realization.

2. Dependency, association, aggregation and combination

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 * 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

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.