Five relationships between classes in UML and their expressions in code

Source: Internet
Author: User

What is a class?

You can combine a certain type of things into a class.

Class has many refining perspectives. You need to select appropriate perspectives based on the system objectives and business scenarios to summarize things.

What is a class chart?

A Class Diagram may be the most commonly used in UML.

Like other charts, the basic syntax of a class chart is not complex. It may take a day or two to grasp it. However, it may take years to flexibly use the class chart.

Class diagrams are an important tool for training OOA (OO analysis) and OOD (OO design) Ideas and help improve OOA and OOD ideas.

This blog post focuses on the relationship between classes in a class diagram and the implementation form of this relationship in the code. The reason for writing this article is: There are a lot of materials on the Internet about the syntax rules of UML class diagrams, but there are few articles about the implementation form of relations in the code. This is easy to understand: All types of books such as UML syntax specifications are exported, and many on the webpage are copies of various syntax specifications; the relationship between classes is reflected at the code level. The difference between the two is that one requires no brains, just copy it, and the other requires brains.

Relationship between classes

The relationship between classes can be divided into the following five types based on the relationship strength:

Dependency --- Association --- aggregation --- composition --- Generalization)

1. Dependency)

The dependency is represented by a dotted line and an arrow, as shown in:

 

This example may not be very good (the animal body has water,), for another:

There is no relationship between person and computer. However, due to occasional requirements, person must use computer. In this case, person depends on computer.

Dependency is the least coupled relationship among the five relationships.

Class A must reference Class B to complete a function. Class A depends on Class B. C # Two-way dependency, that is, mutual reference, is not recommended.

The preceding dependency is represented in the Code: neither of these two link classes adds any attribute.

So how does the person class use the computer class? There are three methods:

Three forms of dependency:

1. The computer class is public, and the person class can call it.

2. If the computer class is a local variable of a method in the person class, the person class can call it. The Code is as follows:

Person has a programing method, and the computer class is used as the variable of this method.

Note the life cycle of the computer class. It is instantiated only when the programing method is called.

It is the most important thing to hold the computer class as a method of the person class rather than the person class.

3. The computer class is used as a parameter or return value of a method in the person class.

The computer class is held by a method of the person class, and its lifecycle ends with the completion of the method execution.

One of the three methods must be used in the dependency.

2. Association)

An association is indicated by a solid line and an arrow. Indicates that the relationship between classes is stronger than the dependency.

For example, the relationship between water and climate is as follows:

The Code is as follows:

It can be seen that the climate class is added to the water class attribute.

Associations include one-way Association, two-way Association, self-Association, and multi-dimensional association. The last three can be left empty.

Unidirectional Association:

Bidirectional Association:

Self-Association:

Multi-dimensional association:

Differences between Association and dependency:
  • From the perspective of adding class attributes:

No attribute is added to any of the dependent classes. One of these classes serves as a parameter or return value for the method of another class, or as a variable of a method.

Two classes with associations, one of which becomes the attribute of another class, and the property is a more closely coupled, more persistent hold relationship.

  • From the perspective of link lifecycle:

Dependency is generated only when the class method is called and ends with the end of the method.

Associations are generated when the class is instantiated. When the class is destroyed, the link ends. Compared with dependency, the associated relationship has a longer lifetime.

3. Aggregation 4. Composition)

Let's cite the example of the big goose in Cheng Jie's "big talk Design Model:

Geese like to be lively and afraid of loneliness, so they have been living in a group, so they have a group. Each of them has its own group, and each group has many geese, this relationship between geese and geese can be calledAggregation.

In addition, each wild goose has two wings. The relationship between the wild goose and the wild goose wings is calledCombination.

This is visible:

The aggregated relationship is obviously not closely formed, and the geese will not survive because their Group Owners disband the geese;

However, yundun cannot survive independently from the yundun-the composite class has the same lifecycle.

Aggregation relationship diagram:

Combination relationship diagram:

The Code is as follows:

The difference between the two relationships is:

1. Different Constructors

  • The constructor of an aggregate class contains instances of another class as parameters.

Because the constructor transmits an instance of another class, the geese can exist independently from the geese.

  • Constructor of a combination class contains instantiation of another class

Because the constructor is instantiated, the two are closely coupled, and the wings cannot be separated from the geese.

2. Different information Encapsulation

In the aggregation relationship, the client can understand the goosegroup class and goose class at the same time, because they are independent.

In the composite relationship, the client only recognizes the geese and does not know the existence of the wings, because the wings are tightly encapsulated in the geese.

5. Generalization)

Generalization is an academic name. Generally speaking, it includes the inheritance relationship between classes and the implementation relationship between classes and interfaces.

Generalization between classes

Interface implementation

Off-topic: This kind of things cannot be discussed on paper. It requires a lot of practice, practice-understanding-practice-recognition, and constant criticism and self-criticism.

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.