The class diagram

Concept and analysis

  • Class diagram is composed of four parts: class name, attribute layer, method layer and property layer. Except the class name must exist, other layers can be omitted.

  • The class name in the class diagram is concrete if it is in font, or abstract if it is in italics

  • The second layer of the class diagram is the property layer. The properties can be public, private, or protected. If the left side of a property is a plus sign (+), the property is public, if the left side is a minus sign (-), the class is private, and if the left side is a hash sign (#), the property is protected.

  • The third layer is the method layer. The method layer is also divided into public, private, and protected. The difference is the same as in the property layer. Also, if there is an underscore under a method, the method is static.

  • The only difference between the class diagram of an interface and the class diagram of a class is the addition of interface to the class name, indicating that the class diagram is a class diagram of an interface.

Relationships in class diagrams

There are lines between classes that indicate the relationship between them. There are several relationships that can be established between classes, classes and interfaces, and interfaces and interfaces: generalization, association, aggregation, composition, and dependency. In addition, these relationships are static.

  • Generalized relation: Generalization relationships represent inheritance relationships between classes (extends in Java), interfaces between interfaces (extends keyword in Java; an interface can extends keywords, but cannot implements any interface), Or the implementation relationship between the class and the interface (implements keyword in Java). So the implementation of general relationships in Java is extens or implements, extends describes class-to-class, interface to interface, and implemens describes class-to-interface relationships.

(The figure above implements the general relationship between class and class, class and interface, and interface to interface)

  • Association relationships: Association relationships are joins between classes that allow one class to know the properties and methods of another class. Simultaneous associations can be bi-directional or unidirectional. Association relationships in Java are implemented through instance variables in classes, such as the class related to the driver and the car, the class related to the question and the answer. And each endpoint of an association in the relationship can have a cardinality, meaning that the class at that end can have several instances, which is the equivalent of one-to-one, one-to-many, and many-to-many problems in Java. For example, a driver can have more than one car, one question can have more than one answer, and one answer may have more than one question.

图片(One-way relationship)

(Bidirectional relationship)

  • Aggregation relationship: Paradigmatic relations is one of the types of relationships, is a strong correlation, which can be understood as a whole must need to individuals, such as a computer must have a CPU, hard disk, motherboard, memory, etc., so as the paradigmatic relations is the computer class must be associated with the CPU, hard disk, motherboard, memory, etc must be kind, so as to ensure the integrity of computer as a whole. Therefore, the difference between the aggregation relation and the association relation lies in the judgment of wholism and individual. The classes involved in the association relation are all equal, but the classes involved in the aggregation relation are not at the same level. One of them represents the whole and the other represents the individual.

(Manifestation of the aggregation relationship)

  • Composition relation: Composition relation is also a kind of association relation, and it is stronger than aggregation relation, composition relation is strong mainly because the object representing the whole is responsible for representing the life cycle of some objects, and composition relation cannot be shared. Objects that represent the whole need to keep parts alive. For example, the relationship between the Monkey King and his limbs and the cudgel is a cohesive relationship, while the relationship between the Monkey King and his limbs is stronger, a synthetic relationship, because the limbs of the Monkey King are entirely his own responsibility and cannot be shared.

(Expression of synthetic relation)

  • Dependencies: Dependencies are connections between classes. Dependencies are always one-way. A dependency means that a class depends on another class, that is, a class needs to be implemented through the dependent class, thus forming a dependency. For example, the Person class depends on the Car and House classes, so the Car and House classes need to be implemented in a reference to the Person class, and the Person class has no property of type Car or House. The instances of Car and House are passed as arguments to the reference methods in the Person class.

(Representation of dependencies)

Since the Car and House classes are passed as parameters to the reference methods in the Person class, in Java this dependency is reflected in local variables, request parameters, and calls to static methods.