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

How to implement UML Class Diagram

2025-04-10 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 "how to realize the UML class diagram". The editor shows you the operation process through the actual case, and the operation method is simple, fast and practical. I hope this article "how to realize the UML class diagram" can help you solve the problem.

UML class diagram

The relationship between classes plays an important role in understanding object-oriented, and this question has been often asked in interviews in the past. I will introduce it here.

There are the following relationships between classes:

(1) Generalization

(2) Association (Association)

(3) dependence (Dependency)

(4) aggregation (Aggregation)

UML diagram and application code examples:

1. Generalization (Generalization)

[generalization]

The UML class diagram generalizes the inheritance relationship between classes, the inheritance relationship between interfaces, or the implementation relationship between classes and interfaces. The generalized relationship is directed from the subclass to the parent class, as opposed to the inheritance or implementation method.

[specific performance]

Parent class instance = new subclass ()

[UML diagram] the generalization relationship between Animal class, Tiger class and Dog class

[code performance]

ClassAnimal {classTigerextendsAnimal {} publicclassTest {publicvoidtest () {Animala=newTiger ();}}

two。 Dependency (Dependency)

[dependence]

For two relatively independent objects in the UML class diagram, when one object is responsible for constructing the instance of another object, or depends on the service of another object, the relationship between the two objects is mainly reflected in the dependency relationship.

[specific performance]

Dependencies are shown in local variables, parameters of methods, 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 representation] dependency between Person class and Screwdriver class

[code performance]

PublicclassPerson {/ * * screw * / publicvoidscrew (Screwdriverscrewdriver) {screwdriver.screw ();}}

3. Association (Association)

[Association]

For two relatively independent objects in the UML class diagram, when there is a fixed corresponding relationship between the instance of one object and some specific instances of another object, there is an association between the two objects.

[specific performance]

The association relationship is implemented using instance variables.

[practical example]

For example, customers and orders, each order corresponds to a specific customer, each customer corresponds to a specific order; another example is a company and employees, each company corresponds to some specific employees, each employee corresponds to a specific company

[UML diagram] the relationship between the company and employees

[code performance]

PublicclassCompany {privateEmployeeemployee; publicEmployeegetEmployee () {returnemployee;} publicvoidsetEmployee (Employeeemployee) {this.employee=employee;} / / the company operates publicvoidrun () {employee.startWorking ();}} publicclassPerson {/ * * screw * / publicvoidscrew (Screwdriverscrewdriver) {screwdriver.screw ();}}

(4) aggregation (Aggregation)

[aggregation]

When object An is added to object B and becomes a part of object B, there is an aggregation relationship between object B and object A. Aggregation is a kind of association relationship in UML class diagram, which emphasizes the relationship between the whole and the part.

[specific performance]

Like association relationships, aggregation relationships are implemented through instance variables. There is no way to distinguish between relevance relation and aggregation relation in syntax, but the difference between them can be better distinguished semantically.

[differences between association and aggregation in UML class diagrams]

The main results are as follows: (1) the two objects involved in the relationship are at the same level. For example, people and bicycles are related, not aggregative, because people are not made up of bicycles.

The two objects involved in the aggregation relationship are at the level of inequality, one represents the whole and the other represents the part. For example, there is an aggregation relationship between the computer and its monitor, keyboard, motherboard and memory, because the motherboard is part of the computer.

(2) for two objects with aggregation relations (especially strong aggregation relations), the whole object will restrict the life cycle of its constituent objects. The object of part of the class can not exist alone, its life cycle depends on the life cycle of the object of the whole class, when the whole disappears, the part also disappears. For example, if Zhang San's computer is stolen, then all the components of the computer no longer exist, unless Zhang San removes some computer components (such as hard disk and memory) beforehand.

The aggregation relationship between computers and components

[code performance]

PublicclassComputer {privateCPUcpu; publicCPUgetCPU () {returncpu;} publicvoidsetCPU (CPUcpu) {this.cpu=cpu;} / / turn on the computer publicvoidstart () {/ / cpu operation cpu.run ();}} this is the end of the introduction about "how to implement the UML class diagram". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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