Basic UML knowledge

Source: Internet
Author: User

(This is an article about UML that I wrote a long time ago. I will share it with you now)

It is necessary to understand the basic knowledge of the association between classes, because these relationships are like the foundation of building a house and are the foundation of object-oriented programming.

There are six types of relationships in the class: Association, aggregation, composition, generalization, and realization) the dependencies are described below.

 

Dependency relationship

Dependency is the weakest link between objects and a temporary link. Dependent objects depend on the objects to complete certain functions, changes in the behavior of dependent objects will affect the result output of dependent objects. The Code generally refers to the call relationship between other objects established by local variables, function parameters, and return values.

In the class chart, use a dotted line with an arrow to indicate that the arrow points from the class to the dependent class, as shown in:

For example, if a person wants to use a spoon for dinner, there is a dependency between the person and the spoon. The size of the spoon affects the speed or mood of the person eating. The dependency diagram at this time is as follows:

 

Association

Association indicates the relationship between two objects. In other words, Association defines the multiple objects. Association is a stronger relationship than dependency, there is no contingent or temporary dependency, and the relationship is generally long-term. In addition, the relationship between the two parties is generally equal, and the association can be one-way and two-way, the associations we often use are one-to-one, one-to-many, many-to-one, and many-to-many. They are represented by a straight line arrow, the arrow points to the dependent object:

 

Unidirectional Association

 

Bidirectional Association

The numbers at both ends of the arrow indicate the quantitative relationship between two correlated objects. The following is an example.

One-to-one

For example, in the relationship between a wife and her husband, a wife has only one valid husband and only one valid wife. They represent two-way association, as shown in the following figure:

 

One-to-many, multiple-to-one

If a company has multiple employees and multiple employees belong to the same company, the relationship with employees is one-to-many from the company's perspective, if we look at the relationship with the company from the employee's perspective, it is the many-to-one relationship, as shown in the following figure:

 

Many-to-many

If a professional photographer signs contracts with multiple newspapers so that they can use their photos, and each newspaper signs contracts with different photographers, that is to say, each newspaper has multiple contracted photographers. This relationship belongs to the many-to-many relationship, which can be shown as follows:

 

 

Aggregation relationship

Aggregation is also a type of association, but it represents a stronger association. It is used to represent the relationship between the whole and the part, indicating the direct relationship between the object and the object, when an object "owns" another object, you can use an aggregation Relationship between them. Therefore, an aggregation relationship is usually called a "owns" relationship.

The aggregation relationship is shown in the hollow diamond + solid line + Arrow Diagram. One side of the diamond refers to the owner, and the other side of the solid line is the owner:

 

The numbers at both ends of the arrow indicate the quantitative relationship between two correlated objects. The following is an example.

 

One-to-one

Every car has an engine, and every car usually has only one engine. The scenario here is suitable for the use of an aggregated graph:

One-to-many, multiple-to-one

If a car can drive away, it will certainly not work without wheels. That is to say, a car has wheels and generally has four wheels. They are also suitable for expressing in an aggregate relationship:

A car has an engine and four wheels. The relationship between them is shown as follows:

Many-to-many

 

 

 

Composition relationship

A composite relationship is a special aggregation relationship, but it has a stronger relationship than an aggregate relationship. If an object contains another or some objects, if no container object exists, if the contained object cannot exist, this relationship is called a composite relationship; in a composite relationship, the object representing the whole is responsible for managing the entire lifecycle of the object representing the individual/part. If the object representing the whole is destroyed/destroyed, objects that represent an individual/part of the object will be destroyed or damaged.

The difference between an aggregation relationship and a combination relationship is that Objects representing an individual/part of an aggregation relationship may be shared by multiple objects representing the whole, it may not necessarily be destroyed or destroyed as a whole object is destroyed or destroyed.

The composite relationship is represented by solid diamond + solid line + Arrow:

 

The numbers at both ends of the arrow indicate the quantitative relationship between two correlated objects. The following is an example.

One-to-one

Generally, each of us has only one mouth on the head, and one mouth only belongs to one head. If there is no head, the mouth will be gone, and the head must be responsible for the existence of the mouth, they conform to the definition of the composite relationship and belong to a one-to-one relationship. They can be used as an example to indicate the composite relationship between them:


One-to-many, multiple-to-one

Each of our hands has five fingers. The fingers must exist in the hands. If the hands do not exist, the fingers do not exist. Their definitions comply with the definition of the composite relationship. One hand can have multiple fingers. At this time, the relationship between the hand and the finger is one-to-many. if the subject is a finger, multiple fingers belong to one hand at the same time, this is the relationship between multiple-to-one. As shown in the following figure:

Many-to-many

In the real world, although many-to-many combinations exist, for example, a pair of connected sisters are two people, they only have two feet, two people can control their feet. The feet belong to two people, which is also a combination of many to many.

But does the many-to-many combination exist in our system?

 

Generalization

Generalized represents the is-a relationship, which is the most coupled relationship between objects. Subclass inherits all the details of the parent class and has the ability to add its own new functions, inheritance is the most common relationship between a class or interface and an interface. in Java, you can directly use the inheritance keyword extends in the language to specify the identifier.

In the UML class diagram, a solid line with a triangular arrow is used. The arrow points from the subclass to the parent class, as shown in:

Inheritance between classes

Inheritance between interfaces

A subclass can only inherit one parent class, because it inherits the Child class of the parent class and can only be a special type of the parent class, such as we humans, it can only be an animal or a plant. It cannot be an animal or a plant. The inheritance relationship diagram is as follows:


However, for inheritance between interfaces, sub-interfaces can inherit multiple parent interfaces at the same time, because interfaces represent inheritance between functions or actions. For example, we humans can have multiple behaviors, such as eating, sleeping, and walking. Each behavior can be represented by a parent interface. The inheritance relationship is shown below;

Implementation (realization) relationship

The implementation relationship in the class diagram represents the relationship between interfaces and implementation classes. It refers to the function of a class to implement one or more interfaces and is also a very common relationship, in Java, you can directly use the implementation keyword implements in the language to clearly identify.

In the UML class diagram, use a dotted line with a triangular arrow to indicate that the arrow points from the implementation class to the interface, as shown in:

 

A subclass can implement multiple interfaces. This can be understood as a subclass has all the functions of the implemented parent class, and different parent classes can implement different functions at the same time, if a person has such a function as eating, walking, and talking, the implementation diagram is as follows:

Summary:

Among these six relationships, inheritance and implementation reflect the vertical upper and lower-level relationship between a category and a class, or between a class and an interface; the relationships of combination, aggregation, association, and dependency reflect the horizontal reference relationship between classes and classes, or between classes and interfaces, which is difficult to distinguish in some cases, in addition, it is not easy to accurately locate the relationships between many things. These relations are semantic-level, so these relations may not be fully distinguished from the code level. However, in general, the dependency strength of a combination, aggregation, association, and dependency relationship is: Combination> aggregation> association> dependency.

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.