UML class diagram Summary

Source: Internet
Author: User

This articleArticleNice too


In fact, I have always thought that UML is a very useless thing and never learned much. It is enough to look at the UML diagram drawn by others occasionally. Dramatically, I have seen the most UML companies (actually the only one) and the worst and most Sb companies I have ever met in my career, the entire development process should be based on UML, And the last project is messy. Of course, I am not saying that UML itself is not good. Good things are good things to use, but for UML, I think if the people or organizations used are not good at their own level, I still have to install B to use this stuff, but it can only be worse and worse.

Now I think UML is a technology that has been eliminated... At least not as admired as in the past few years. However, you can understand the UML diagram. This is also the main purpose of this Article. To sum up, let's take a look at UML.

The figure in this article is self-drawn, and the text is mostly extracted from the previous contents.

========================= Body ================================ =


A uml class diagram is first used to describe a class. Class is represented by a box, which is separated into several columns.

Attribute on, method under. The name is left and the type (return value type) is back. Abstract class abstract methods use italics"

Symbol of visibility: + public,-private, # protected ,~ Package.


Classes are actually very simple. What is important is the relationship between classes.

UML classifies the relationships between classes into the following five types.

● Association: there is a specific correspondence between instances of Class A and Class B (see the following for details) -- <> <-- <|>
● Dependency: services provided by Class A and Class B ----->
● Aggregation: Class A is the overall Class, Class B is the local class, and Class A objects are composed of Class B objects.
● Generalization: Class A inherits Class B
● Implementation: Class A implements the B Interface

The associated dependency and aggregation have different meanings. You need to determine the relationship between the two based on the specific business logic. The strong-weak relationship is clustering> association> dependency. Dependency is the weakest link. That is to say, if A is associated with B, A and B must be dependent. Clustering is complex and well differentiated from the other two relationships. Association emphasizes the relationship between the two. That is to say, if A is associated with B, then the object of A is to use a B instead of any B, that is to say, the object of a and the object of B areAssociated. Therefore, if A is associated with B, object A needs to save a reference to object B. For example, person and idcard are associated. The person object should have a reference pointing to the idcard object. It cannot be any idcard.. The dependency is weaker than the correlation relationship. It only indicates that a uses B, and it does not matter which B is used. For example, the system. Out. println (string Str) method,Depends on string, but does not depend on a specific stringThis is the dependency.

Association refers to the specific correspondence between classes, which is expressed by arrows with solid lines in UML.

Compare and associate classes by quantity
There are three types:
● One-to-one Association

● One-to-Multiple Association

● Multiple-to-Multiple Association

Note: The Association can also be divided into one-way Association and two-way Association.

Dependency refers to the call relationship between classes, which is expressed by arrows with dotted lines in UML. If Class A is an attribute or method of Class B, or Class A is responsible for instantiating Class B, Class A can be said to depend on Class B. Unlike associations, you do not need to define B-type attributes in Class. Dependency is a weak relation in UML. It is very common. Which class is used to supplement dozens or even more classes? If recursive Calculation continues, there will be more dependencies.


 Clustering is a closer relationship than combination. A combination refers to the correspondence between two classes. Clustering means that these two classes are planned into a system and have a closer aggregation relationship.

For example, person and employee card. If the system needs to define an employ type. In this system, this type aggregates the person and employee card.

Or point an empty arrow to the front,

Clustering refers to the relationship between the whole and the part, which is expressed by a diamond arrow with solid lines in UML.
Clustering can also be divided into two types:
● The clustered subsystem can be disassembled and replaced. This is a common clustering relationship.
● The clustered subsystem cannot be disassembled or replaced. Such aggregation is called a strong Clustering or composition relationship.
Note: strong aggregation (composition) can be expressed by solid diamond arrows with solid cables.

Generalization refers to the inheritance relationship between classes. In UML, it is represented by a triangle arrow with a solid line.

Implementation refers to the relationship between classes and interfaces. In UML, a triangular arrow with a dotted line is used.

=========================================================== ================================
What is the difference between dependency and aggregation \ combination and association? Association is a kind of relationship between classes. For example, teachers teach students, husbands and wives, and water bottles are a kind of relationship. This relationship is very obvious and can be obtained directly through analysis in the problem field. Dependency is a weak Association. When one class uses another class, but the relationship with the other class is not too obvious (it can be said that the class is "uses ), we can regard this relationship as dependency, and dependency can also be said to be an accidental relationship, rather than an inevitable relationship, that is, "I accidentally used it in a method, but in reality, I have little to do with it ". For example, I have nothing to do with a hammer, but I used it when I had to nail it. This is a dependency that relies on a hammer to complete the dingtalk. A combination is a whole-part relationship. In the problem domain, this relationship is obvious and can be obtained through direct analysis. For example, tires are a part of a car, leaves are a part of a tree, and hands and feet are a part of the body. This relationship is very obvious as a whole-part relationship. The preceding relationships (Association, aggregation/combination, and dependency) areCodeIt may appear in another class in the form of pointer, reference, value, etc., but logically they have the above differences. It should also be noted that the so-called relationships are valid only in a problem domain. If the problem domain is left, these relationships may not be valid, for example, they may be in a problem domain, I am a carpenter and need to work with a hammer. The whole problem may be described as how to pin the table, chair, and cupboard with a hammer. Since the whole problem is described as follows, the relationship between me and the hammer is not only accidental dependencies, but the relationship between me and the hammer has become very close and may rise to a combination relationship (reminds me of the sword of martial arts novels, the sword is dead ...). This example may be a bit ridiculous, but it is also to illustrate the truth that, like links and classes, they are all established in a problem field and leave this problem field, they may no longer exist.

Differences between dependency and association:
① From the perspective of whether the class attributes are added: 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 the link life cycle: 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.

Combination and aggregation
In simple terms, combination and aggregation A and B. If a combines B, the lifecycle of B is bound to A, or controlled by.

PublicA {
Pubic A (B ){
//Important: B is passed in, not created by.
This. B=B ;}


PublicA {
Pubic (){
//Important: B is created by.
B= NewB ();

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: 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.