Common usage of UML class diagram

1. UML class diagram icon example

Insert picture description here


2. Specific analysis of the content in the above pictures

1. Representation of the class

Insert picture description here

  • First look at the animal rectangle, which represents a class.
  • The class diagram is divided into three layers:
    • The name of the first-level display class, if it is an abstract class, it will be displayed in italics;
    • The second layer is the characteristics of the class, that is, attributes;
    • The third layer is the operation of the class, that is, the method.
  • The'+' in front of attributes and methods means public;'-' means private;'#' protected.

2. Representation of the interface

Insert picture description here

  • The interface representation is somewhat different from the class, and there is a <<interface>> display on the top of the interface.
  • The first line is the name of the interface.
  • The second line is the method of the interface.

The interface also has a representation method, commonly known as the lollipop representation, as follows:
Insert picture description here
That is to say, the Donald Duck class implements the speaker interface, and the speech () method in the interface is implemented in the Donald Duck implementation class.

3. Dependency relationship

The dependency is represented by a dotted arrow, as follows:
Insert picture description here
As long as the other party is used in the class, there is a dependency between them. details as follows:

  • Class member properties
  • Method return type
  • Types of parameters received by the method
  • Used in the method

Example: In a method of class A, the parameter type of the method is class B and class C, then class A is dependent on class B and class C. The code is as follows:

public abstract class Animal {
    
    
    public void metabolism(Oxygen oxygen,Water water){
    
    
    }
}

4. Inheritance relationship (generalization relationship)

The inheritance relationship is represented by a hollow triangle + a solid line, as follows:
Insert picture description here

  • The generalization relationship is actually the inheritance relationship, which is a special case of the dependency relationship.
  • If class A inherits class B, we say that there is a generalization relationship between A and B.

5. Realize the relationship

The implementation interface is represented by a hollow triangle + dashed line, as follows:
Insert picture description here

  • The realization relationship is actually the realization of the B interface by class A, which is a special case of the dependency relationship.

6. Association (association) relationship

The association relationship is represented by a solid arrow, as follows:
Insert picture description here

  • Association relationship is actually the connection between class and class, it is a special case of dependency relationship
  • Association is navigational: that is, two-way relationship or one-way relationship
  • The relationship has multiple characteristics: such as "1" (representing one and only one), "0..." (representing 0 or more), "0, 1" (representing 0 or one), "n...m" ( Means n to m can be), "m...*" (means at least m).
  • One-to-one relationship
    Insert picture description here
public class Person {
    
     
	private IDCard card;
}
public class IDCard{
    
    }
  • Two-way one-to-one relationship
    Insert picture description here
public class Person {
    
     
	private IDCard card;
}
public class IDCard{
    
    
	private Person person 
}

Example: The penguins mentioned above need to know the laws of the climate before they can carry out some activities, such as eggs, predation, etc. When one class refers to another class, this time is an association relationship, such as the following code:

Class Penguin extends Bird {
    
    
	//在企鹅Penguin类中引用了气候Climate类
	private Climate climate;
}

7. Aggregation relationship

The aggregation relationship is represented by a hollow diamond + solid arrow, as follows:
Insert picture description here

  • The aggregation relationship is a weak "ownership" relationship.
  • Aggregation represents the relationship between the whole and the part, and the whole and the part can be separated.
  • The aggregation relationship is a special case of the association relationship, so it has the navigation and multiplicity of association.
  • For example, a computer consists of a keyboard, a monitor, a mouse, etc.; the various accessories that make up the computer can be separated from the computer
    Insert picture description here

Example: For example, a flock of wild geese and wild geese, each wild geese belongs to one flock of wild geese, and a flock of wild geese can have multiple wild geese, so they meet the aggregation relationship, as shown in the following code:

public class WideGooseAggregate {
    
    
	//在雁群WideGooseAggregate类中,有大雁数组对象arrayWideGoose
    private WideGoose[] arrayWideGoose;
}

8. Composition relationship (also can be translated as composition relationship)

The composite relationship is represented by a solid diamond + solid arrow, as follows:
Insert picture description here

  • The composite relationship is a strong "ownership" relationship.
  • Combination relationship: It is also the relationship between the whole and the part, but the whole and the part cannot be separated, which reflects the strict relationship between the part and the whole, and the life cycle of the part and the whole is the same.
  • There are a number 1 and 2 at both ends of the composite relationship line, which is called the cardinality, indicating that the class at this end can have several instances:
    • In this example, 1 and 2 indicate that a bird has two wings;
    • If a class may have countless instances, use n to represent;
    • In addition, association relationships and aggregation relationships can also have cardinality.
  • Simply put, instantiate class B in the constructor in class A (create class B objects), and generate them simultaneously.
    Example: For example, birds and wings, because they are part and whole, and the life cycle of wings and birds are the same, the following code:
public class Bird {
    
    
    private Wing wing;
    public Bird(){
    
    
    	//在Bird类的构造器中创建Wing类对象
        wing = new Wing();
    }
}

Guess you like

Origin blog.csdn.net/MrYushiwen/article/details/112232934