Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the types of UML class diagram relationships?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "which kinds of UML class diagram relations are divided into". The editor shows you the operation process through actual cases, the operation method is simple and fast, and it is practical. I hope this article "which kinds of UML class diagram relations are divided into" can help you solve the problem.

UML class diagram relationships are divided into four types: generalization, dependence, association and implementation, and association relations can be refined into aggregation and combination.

1. Generalization (Generalization)

Generalization is the relationship between a parent class and a subclass, which inherits all the structure and behavior of the parent class. You can add new structures and behaviors to the subclass, or you can override the behavior of the parent class.

The generalized relationship represents the inheritance relationship between class and class, interface and interface. The arrow in the relationship points from the subclass to the parent class. In Java, the extends keyword is used to express this relationship directly.

Example:

PublicabstractclassEmployee {

}

PublicclassProgrammerextendsEmployee {

}

two。 Dependency (Dependency)

Dependency relationship in UML class diagram is a kind of usage relationship, and the change of a particular thing may affect the thing that uses it, and vice versa. Use it when you want to show one thing using another.

Typically, dependencies are reflected in the fact that the methods of one class use another class as a parameter.

Dependency: for two relatively independent objects, when one object is responsible for constructing an instance of another object, or relying on the service of another object (method function), the relationship between the two objects is mainly reflected in the dependency relationship.

[specific performance]

The dependency relationship is shown in 1. Local variables (Question: this refers to the local variables of the method, or the member variables of the class, or both), 2. Parameters of the method, 3. And calls to static methods

[practical example]

For example, if you are going to screw, do you need to use (that is, rely on) a screwdriver (Screwdriver) to help you with the work of screw?

[UML performance]

Dependency

[code performance]

PublicclassPerson {

Publicvoidscrew (Screwdriverscrewdriver) {

Screwdriver.screw ()

}

}

3. Association (Association)

In the UML class diagram relationship, association is a structured relationship, which means that one object is related to another object. Given two associated classes, the object of one class can be obtained from the object of the other class. Relevance has dualistic and pluralistic relations. Binary relationship refers to an one-to-one relationship, while pluralistic relationship is an one-to-many or many-to-one relationship. It is common to connect the same class or two different classes with solid lines. Use associations when you want to represent structured relationships.

Associations are implemented through member variables of the class.

3.1 aggregation (Aggregation)

Aggregation is a special association in UML class diagram relationships. It describes the "hasa" relationship, indicating that the whole object owns part of the object.

There is no way to distinguish between relevance relation and aggregation relation in syntax, but the difference between them can be better distinguished semantically. Aggregation is a strong relationship, which emphasizes the relationship between the whole and the part.

Aggregation is a form of association that represents the global / local relationship between two classes. Aggregation implies that the whole is conceptually at a higher level than the local, while association implies that the two classes are conceptually at the same level.

Like association relationships, aggregation relationships are implemented through member variables of the class.

3.2 combination (Composition)

Composition is a form of aggregation in the UML class diagram relationship, which has a stronger ownership relationship and emphasizes that the life cycle of the whole and the part is consistent. Overall responsible for the management of the life cycle of the part. If the whole is destroyed, the part must be destroyed with it, and if the owner is copied, the part must be copied together.

Like the association relationship, the combination relationship is realized through the member variables of the class.

The difference between aggregation and combination in UML class diagram relations is that aggregation relations are "has-a" relations, and combination relations are "contains-a" relations; aggregation relations indicate that the relationship between the whole and the part is relatively weak, while the combination is relatively strong; the objects that represent part of things in the aggregation relationship have nothing to do with the lifetime of objects that represent aggregate things, and objects that represent parts of things may not necessarily be deleted once the aggregate object is deleted. Once the combined objects are deleted in the combination, the objects that represent some things are deleted at the same time.

We use simple examples to illustrate the difference between aggregation and combination. "the country is broken and the family is destroyed", and the family is naturally gone, and it is obvious that "country" and "home" are also combinatorial relations. On the contrary, there is an aggregate relationship between the computer and its peripherals, because the relationship between them is relatively loose, the computer is gone, the peripherals can exist independently and can be connected to other computers. In aggregation relationships, parts can exist independently of aggregations, and partial ownership can be shared by several aggregations, such as printers that can be shared by colleagues in the office.

The difference between association and aggregation is mainly in semantics, the two related objects are generally equal, for example, you are my friend, aggregation is generally not equal, for example, a company contains a lot of employees, its implementation is similar. The difference between aggregation and composition is different in semantics and implementation. there is a great relationship between the lifetime of the two combined objects. the combined object is created at the same time or after the creation of the combined object and destroyed before the combined object is destroyed. Generally speaking, the combined object can not exist independently from the combined object, and can only belong to one combined object, such as the version of a document, must depend on the existence of the document, and can only belong to one document. Aggregation is different, where the aggregated objects can belong to multiple aggregated objects, for example, an employee may belong to multiple companies.

I want to give you a popular example.

There is a composition relationship between you and your heart (the heart only belongs to you)

There is an aggregation relationship between you and the book you bought (the book may be someone else's)

It's an association relationship between you and your friends.

4. Implement (Realization)

The implementation relationship in the UML class diagram relationship specifies a contract between two entities. In other words, one entity defines a contract, while another entity guarantees the performance of the contract.

There are many kinds of inter-class relations, which can be divided into two categories: vertical relationship and horizontal relationship.

Vertical relationship is inheritance relationship, its concept is very clear, and it has become one of the three important features of OO, which is not discussed too much here.

Horizontal relationships are more subtle and can be broadly divided into four categories according to UML's recommendations:

Dependency (Dependency)

Association (Association)

Aggregation (Aggregation)

Combination (Composition)

There is no objection to their strength: dependence

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report