preface

Let time accumulate real value

I’ve been seeing some class diagrams lately, probably because I don’t see enough of them, and I don’t know what they mean. You need to go back and forth to Baidu. In order to save time looking at UML class diagrams, I made this note for my own reference.

How do you distinguish between classes

The lines between class diagrams represent the relationships between classes. There are basically 6 kinds of connections.

For simplicity, instead of using a real class diagram, use rectangles instead.

Generalization relation — solid line hollow triangle arrow

To put it bluntly, class B inherits class A, and B and A have A generalization relationship

In Java, inheritance relationships are often represented by the extends keyword

interface A{

.

}

class B extends  A{

.

}

Copy the code
Realize the relationship — dotted line hollow triangle arrow

Simply put, a class implements an interface for another class.

Implementing relationships in Java is often represented by the implements keyword

class A{

.

}

class B implements A{

.

}

Copy the code
Dependencies — dotted arrow:

Represents dependencies, with arrows pointing to dependent entities (dependencies, where one node is part of another node)

In Java, dependencies often take the form of local variables, method parameters, and so on

class A{

.

}

class B {



    // Method A depends on class A

    public void methodA(A a){

.

    }

}

Copy the code
Association relation — solid arrow:

Association, representing connections between objects. Associations enable one object to know the properties and methods of another object.

In Java, associations are often represented using instance variables (class attributes)

class A{

.

}

class B {

    private A a;

.

}

Copy the code
Aggregation relationship — hollow diamond + solid line + arrow

Aggregation relation is a special case of correlation relation, strong correlation relation, and the relation between individual and whole.

Note: the individual and the whole are separable. For example, departments and employees (employees can be independent from departments)

In Java, aggregate relationships are also represented using instance variables and typically use a collection to load individuals (one-to-many relationships).

class A{

.

}

class B {

    private List<A> aArray;

.

}

Copy the code
Combination relation – solid diamond + solid line + arrow

Aggregative relation can be regarded as an enhanced version of aggregative relation. The individual cannot exist independently from the whole, just like the relationship between man and hand.

Syntactic relation and combinatorial relation cannot be distinguished directly, but only logical relation between objects can be distinguished.

added

Generalization and realization relationships are common and easy to recognize. The coupling degree among dependency relation, association relation, aggregation relation and combination relation increases successively.

How to draw UML class entities

Generally speaking, there are two kinds of commonly used class diagrams, one is the interface class diagram, the other is the ordinary class diagram

Generic class class diagram

The class diagram is divided into three squares, and different squares represent different contents.

The first box — the class name
The second square — attributes of the class

There are many rows, and each row corresponds to a property

+ attribute1:type = defaultValue

Copy the code
  • The position of the first “+” is used to indicate the visibility of the property. Among them:

  • “+” for public

  • “-” corresponds to private

  • The “#” corresponds to protected

  • Attribute1 represents the name of the attribute

  • Type corresponds to the type of the attribute

The third square — the method of the class

Like properties, a line represents a method, and method visibility is also represented by “+”, “-“, and “#”.

The argument list needs to be enclosed in parentheses for the method

Interface class diagram

There are no attributes in an interface class. In contrast to a normal class diagram, the interface class diagram removes the second square.