Basic knowledge of UML

Source: Internet
Author: User
Tags dashed line

(This is a long time ago wrote an article about UML, now put out and share with you)

It is necessary to understand the basic knowledge of the association between classes and classes, as these relationships are like the cornerstone of our building and are the basis for orientation programming.

There are six types of relationships in a class, namely, Association (association) relationships, Aggregation (Aggregation) relationships, combinatorial (composition) relationships, generalization (generalization) relationships, Implement (realization) relationships and dependency (Dependency) relationships, which are described in the following six relationships.

Dependency (Dependency) Relationship

Dependency is the weakest association between objects, is a temporary association, dependent on the object depends on the object to accomplish a certain aspect of the function, the change of dependent object behavior will affect the output of the dependent object, the code is generally referred to by the local variables, function parameters, return value established for other objects of the call relationship.

In a class diagram using a dashed line with arrows, the arrows point from using the class to the dependent class, as shown in the following illustration:

For example, a person to eat to use a spoon, when the person and spoon is a kind of dependency, the size of the spoon will affect the speed of people to eat or mood, etc., at this time the dependency diagram is as follows:

Association (association) Relationship

The association represents the relationship between two objects, in other words, the association defines the multiplicity between the objects, the association is a stronger relationship than the dependency relationship, there is no chance of dependency, the relationship is not temporary, generally long-term, and the relationship between the two sides is generally equal, the association can be unidirectional, two-way, The associations we often use are a pair of one or one-to-many, many-to-one, many-to-many, and they are represented by a straight-line arrow, which is the direction of the dependent object:

One-Way Association relationship

Two-Way Association relationship

The numbers at each end of the arrows represent the number of relationships between two related objects, respectively, as illustrated below.

One

If the relationship between wife and husband, a wife will only have a legitimate husband, a husband will only have a legitimate wife, they reflect the two-way connection, can be shown as follows:

One-to-many, many-to-

If a company will have more than one employee, many employees together belong to the same company, at this time from the company's perspective and employee relations, is a one-to-many relationship, if from the employee's point of view and the company's relationship, that is a lot of a relationship, with the following illustration:

Many-to-many

If a professional photographer will sign with a number of newspapers, so that the newspaper can use TA's photos, and each newspaper will be signed with different photographers, that is, each newspaper will have more than one contract photographer, this relationship is a lot of relationship, can be shown as follows:

Aggregation (Aggregation) Relationships

The aggregation relationship is also one of the associated relationships, but it represents a stronger association, which is used to represent the relationship between the whole and the parts, to represent the direct relationship between objects and objects, and when an object "owns" another object, they can make use of the aggregation relationship, and thus the aggregation relationship is often referred to as " Owning "relationship.

The aggregation relationship is represented by a hollow diamond + solid line + arrow icon, the owner of the side of the diamond, the side of the solid line being the owning person:

The numbers at each end of the arrows represent the number of relationships between two related objects, respectively, as illustrated below.


One

Each car will have an engine, and each car will usually have only one engine, and the scene here is suitable for using the aggregation diagram representation:

One-to-many, many-to-

Cars can drive away, and there's no wheels, that means cars have wheels, and usually cars have four wheels, and they're also suitable for the aggregation relationship:

The car has an engine, there are also four wheels, the relationship between them using the following figure is shown:

Many-to-many

combination (composition) Relationship

A composite relationship is a special kind of aggregation relationship, but it is more strongly related than having an aggregation relationship, when an object contains another object, and if there is no container object, then the contained object is not able to exist, and this relationship is called a composite relationship The object that represents the whole is responsible for the management of the entire life cycle of the object representing the individual/part, and if the object representing the whole is destroyed/destroyed, then the object representing the individual/part will be destroyed/destroyed.

The difference between aggregation and composition is that an object representing an individual/part in an aggregation relationship may be shared by multiple objects representing the whole, and not necessarily destroyed/destroyed by the destruction/destruction of an object representing the whole.

The combined relationship is represented by a solid diamond + solid line + arrow icon:

The numbers at each end of the arrows represent the number of relationships between two related objects, respectively, as illustrated below.

One

Usually we all have a mouth on each head, a mouth will only belong to a head, if the head is not, then the mouth will not have, the head must be responsible for the presence of the mouth, they meet the definition of the combination of relationships, and is a one-to-one relationship, you can use the following diagram to indicate the relationship between them:


One-to-many, many-to-

Each of our hands will have five fingers, the fingers must exist in the hand, if the hands are not, then the fingers will not exist, their definition of the relationship with the definition of the composition. A hand can have more than one finger, at this time to hand as the main body, then the relationship between hand and finger is a one-to-many relationship, if the finger as the main body, many fingers at the same time belong to a hand, at this time is a multi-to-one relationship. The following illustration shows:

Many-to-many

In the real world, although a lot of combination of the situation is present, such as a pair of conjoined sisters, they are two people, but they have only two feet, two people can control the foot, the foot belongs to two people, this situation is also belong to many-to-many combination situation.

But in our system, many-to-many combinatorial relationships exist.

generalization (generalization) relationship

Generalization represents the is-a relationship, which is the most coupling relationship between objects, which inherits all the details of the parent class and has the ability to add its own new functionality, which is the most common relationship between a class and a class or interface and interface. Explicit identification is extends in Java directly using the inherited keyword in the language.

The UML class diagram uses a solid line with a triangular arrowhead, and the arrows point from the subclass to the parent class, as shown in the following illustration:

Inheritance between class and class

Inheritance between interfaces and interfaces

A subclass can inherit only one parent class, because inheriting the subclass of the parent class can only be a particular type of the parent class, such as we humans, can only be an animal or a plant, it is not possible to both animals and plants, at this time the inheritance diagram is as follows:


However, the interface between the inheritance, sub-interface can inherit multiple parent interface, because the interface between the representation of the function or behavior between the inheritance, such as we humans can have a variety of behaviors, such as eating, sleeping, walking, etc., each behavior can be represented by a parent interface, at this time the inheritance relationship diagram as follows;

Implementing (Realization) Relationships

The implementation of the relationship in the class diagram is the interface and implementation of the relationship between the class, refers to a class class implementation of one or more interfaces, is also a very common relationship, in Java directly using the implementation of the language in the keyword implements explicitly identified.

The UML class diagram uses a dashed line with a triangular arrowhead, and the arrows point from the implementation class to the interface, as shown below:

A subclass can implement a plurality of interfaces, this can be understood as subclasses have the implementation of all the functions of the parent class, subclasses can implement different parent classes at the same time to have different functions, such as people have to eat such a function, you can walk such a function, you can have the function of speaking, and so on, the implementation diagram is as follows:

Summarize:

In these six relationships, inheritance and implementation embody a class and class, or the vertical relationship between class and interface, and the combination, aggregation, association and dependence of the four relationships is the class and class, or class and interface between the cross-referencing relationship, sometimes more difficult to distinguish, And there are a lot of things to accurately locate the relationship is not easy, these relationships are semantic level, so from the code level is not necessarily able to completely distinguish between these relationships, but overall, the combination, aggregation, association and dependence of the relationship between the degree of dependence is: Combination > Aggregation > Association > Dependence.

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.