Short

The Class Diagrams can show what member a class have. Significantly, it has capacity of illustrating the relation among these classes. An association has the purpose of relating objects of two classes.

Element of Class Diagrams

Class

image-20200313122252450

  • public : +
  • protect : #
  • private : -

Generalization & Realization

Generalization & Realization

“Generalization” and “Realization” are easy to understand.

Composition & Aggregation

Composition & Aggregation

The relation of them focus on the whole and the part and two objects cooperate together.

The difference between them:

“Composition” emphasize the indivisible relation of the whole and the part such as the company and the department. (department to company) If the company go bankrupt, the department of this company also disappears.

On the other hand, “Aggregation” also point to the relation of the whole and the part. However, it would weaker than “Composition” and they can live without each other. (employee to company) For example, those employees could go to another company when it happened.

Association & Dependency

Association & Dependency

The relation of them focus on equity of two objects.

The difference between them:

“Association” is stronger than “Dependency”. “Dependency” means a divisible or indivisible relation, and it emphasize the “use”. For example, a cat need water, so it can say that a cat “depend on” water.

“Association” have an emphasis on the two objects have a relation. For instance, a man and a woman have a marriage. It also can refer to teacher-student relation.

Moreover, “Association” can be one-way or two-way. A Tom’s wife is Lucy and Lucy’s husband is Tom.

The Intensity of Relation

Generalization = Realization >

Composition > Aggregation >

Association > Dependency

The Code of Relation

Here is the code by java.

It is significant to understand the code of the last four relation.

“Composition”, “Aggregation” and “Association” can be shown by the member of a class.

1
2
3
4
5
6
7
8
# A.java
public class A {
public B b;
}

# B.java
public class B {
}

A points to B.

“Composition”: A and B are respectively department and company.

“Aggregation”: A and B are respectively employee and company.

“Association”: A and B are respectively husband and wife.

By the way, “Two-way Association” can be expressed by the following code.

1
2
3
4
5
6
7
8
9
# A.java
public class A {
public B b;
}

# B.java
public class B {
public A a;
}

“Dependency” can be implement by local variables, parameter of class method and the usage of static method.

Here is a example.

1
2
3
4
5
6
7
8
9
10
# A.java
public class A {
public getB(B b){
System.out.println("B");
}
}

# B.java
public class B {
}

A points to B.

A depend on B.

The relation among Controller, Service and Mapper

I guess a Controller can use more than one Service and a Controller can live without a Service. Thus, it should be the “Aggregation”. The relation of Service and Mapper is the same.

Conclusion

The photo refer to a passage. I think it a good conclusion for Class Diagrams You can see more in the Reference.

img

Reference

UML类图与类的关系详解

[1]Bernhard Rumpe. Modeling with UML[M].Springer International Publishing:2016.

[2]Bernhard Rumpe. Agile Modeling with UML[M].Springer International Publishing:2017.