Summary of the Six Relationships of UML Class Diagrams

[Java design pattern] UML class diagram six relations summary (including code)

[Java Design Patterns] Summary of Six Relationships of UML Class Diagrams (including Code) (2)

[UML class diagram.Association


concept
The association relationship is actually the connection between classes and classes, which is a special case of the dependency relationship

One-way one-to-one relationship
code
public class Person {
private IDCard idCard ;
}
public class IDCard { }

Corresponding class diagram


Two-way one-to-one relationship
code
public class Person {
private IDCard idCard ;
}
public class IDCard {
private Person person ;
}

Corresponding class diagram


summary
The association relationship is easier, emphasizing the connection between classes, and the symbols are represented by straight lines

Aggregation _
concept
The aggregation relationship represents the relationship between the whole and the part, and the whole and the part can be separated

UML class diagram.code

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

public class Computer {
private Mouse mouse ; //The mouse can be separated from the computer
private Monitor monitor;//The monitor can be separated from the Computer
public void setMouse ( Mouse mouse) {
this.mouse = mouse;
}
public void setMonitor ( Monitor monitor) {
this.monitor = monitor;
}
}
public class Mouse{ }
public class Monitor{ }

Corresponding class diagram


summary
The aggregation relationship is a special case of the association relationship, so it has the navigation and multiplicity of the association.

UML class diagram.Composition

concept
The composition relationship is the relationship between the whole and the part, but the whole and the part cannot be separated.

code
According to reality, a person can't have no head, but can have no ID card (can be reissued)

public class Person {
private Head head = new Head( );
private IDCard idCard ;
}
public class IDCard { }
public class Head{ }

Note: If the cascading deletion of IDCard is defined in the program entity, that is, when Person is deleted, it is deleted together with IDCard , then IDCard and Person are combined.

Corresponding class diagram


UML class diagram.summary

If you put private IDCard idCard ; change to private IDCard idCard = new IDCard (); Indicates that IDCard and Person cannot be separated and are combined.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00