The relationship between UML modeling learning 3:UML basic building blocks

Source: Internet
Author: User
Tags dashed line

Today we look at another element of the UML basic building block-the relationship.

in UML, there are relationships that represent the basic diagram symbols, which are: Dependency (dependency), generalization (generalization, also known as inheritance), real

Now (realization) and Association (Association), and the association is divided into common associations (common Association), Aggregation (aggregation, some are called poly

set) and combination (composition).

Dependency (dependency)

A dependency represents the use of another class B for a Class A, which is accidental, temporary, and very weak, but changes in class B

Affects Class A, which is the use a relationship.

table now life, such as someone to cross the river, need to borrow a boat, when the relationship between man and ship is dependent; performance at the code level if Class A

depends on class B, then Class B can be a local variable of Class A, or a parameter to a method of Class A, or a call to a static method.

Illustration: Dashed arrow (arrow pointing to the relying party)

Example:


Description: Animals have several major characteristics, such as metabolism, can reproduce. And animals must have vitality, need oxygen, water and food and so on. Other words

Animals depend on oxygen and water. Between them is a dependency relationship (dependency), denoted by a dashed line and arrows.

Let's look at a code-level example:


Note: If two classes have a structural relationship (association), then there is no dependency (two things generally have this relationship).

Generalization (generalization)

Generalization is the inheritance relationship. Generalization is a special/general relationship in which an object of a special element (child element) can be substituted for an object of a generic element (parent element). With this

method, the child element shares the structure and behavior of the parent element. is a is-a relationship, which manifests itself as the inheritance of classes and classes, interfaces and interfaces.

Generalization refers to the ability of a class (called a subclass, sub-interface) to inherit another class (called a parent, parent), and to add its own new work

Ability, inheritance is the most common relationship between classes and classes or interfaces and interfaces; In Java, such relationships are explicitly identified by the keyword extends,

Timing is generally not controversial.

Diagram: Solid line + Hollow triangle

Example:


Implementation (realization)

The implementation is the semantic relationship before the class element, in which a class element describes another class element that guarantees the implementation of the contract. Implementation refers to a class real

Now interface interface (can be multiple) function, implementation is the most common relationship between the class and interface, in Java, this kind of relationship through the keyword implements

Clearly identified, is generally not controversial at design time.

Illustration: Dashed + Hollow Arrow

Example:


Association (association)

An association describes a structural relationship between two or more classes. Represents a join between a class and a class that enables a class to know the properties and methods of another class.

correlation is a strong dependency of two classes, or the semantic level between classes and interfaces, such as me and my friends. This relationship is stronger than reliance and does not

the contingency of the existence of dependence, the relationship is not temporary, is generally long-term , and the relationship between the two parties is generally equal, and the association can be one-way,

in a code-level way, if the class A is associated with Class B, then Class B is the global variable of Class A (note the global variable, and then look at the dependencies above) .

most Association is a one-way association, which is easier to maintain, about associations, in life we often say that Class A holds class B's references. To be associated with Class B as

A class attribute appears in the association Class A, or the association Class A refers to a type is the global variable that is associated with Class B.

(1) General Association (Common Association)

1) Association name: Describes the nature of the relationship.

2) Role roles: When a class is at one end of an association, the class plays a specific role in the relationship; the role is close to it in the association

Class on the other end of the class to render the responsibility.

3) Multiplicity multiplicity: The multiplicity of associated roles is how many interconnected objects are in an instance of an association.

Diagram: Line

Example:


(2) polymerization (aggregation)

The aggregation relation is a special association relation, it is a strong association relation, he embodies the whole and partial relation, namely the relation of has-a, but the whole and

Parts can be separated, note, can be separated. They can have their own life cycles, some of which can belong to multiple whole objects, or they can be multiple

Overall object sharing, such as computer CPUs, company-to-employee relationships, and so on at the code level, consistent with the correlation, and only from the semantic level

to differentiate.

The two classes of the common relational relationship are at the same level, and the two classes of the aggregation relationship are at different levels, one is the whole and the other is

Part. At the same time, it is a weak "owning" relationship. This shows that a object can contain a B object, but the B object is not part of a object. The concrete manifestation is,

If a is aggregated by B and behaves as a global object with B, but the B object can not be created at the moment a is created, this is a very meaningful statement

The code usually manifests as a dependency injection setter method, that is, a object can create a B object at any time, and then think of this does not reflect the whole and part can be separated

It? You can create a whole without creating a part.

Illustration: Hollow Diamond + solid line + arrow

Example:


(3) combination (composition)

The combination is also a special case of the association relationship, he embodies a contains-a (owned) relationship, which is stronger than aggregation, also known as strong poly

It also embodies the relationship between the whole and the part, but at this time the whole and the part are not divided, the whole life cycle end also means that part of the life week

The end of the period, such as you and your brain; At the level of the code, it is consistent with the correlation, and can only be differentiated from the semantic level; At the code level . ,

If A is set by Group B , then a will contain the global variables of B, and create B at the same time as the creation of a, in the code we usually use constructs function for Real Now,

It is also the implementation of the constructor in the dependency injection.

Illustration: Solid Diamond + solid line + arrow

Example:


Summary

Finally, we summarize, generalization and implementation will not be much, we all understand, is to inherit and implement the interface, focus on the other bar, according to

Lai, ClassB embodies as ClassA local variables, I want to use, with the relationship, no matter, the association, CLASSB embodiment of ClassA

Global variables, whether you use it or not, anyway you know I'm there and hold my quote. Aggregation, which is a special relationship, strengthens the

No, I just know you exist. Aggregations can be conveniently held for multiple classes of references, such as using List<>, so when you find that there are list<> and other collections

Can be expressed using aggregations, such as the structure of the observer pattern. Combination, reflect the strongest relationship, such as people born, must also have a head, or I really

Can't imagine the world.



UML modeling learns the relationship of 3:UML basic building blocks

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.