When drawing a class diagram, it is important to clarify the relationships between classes. The relationships of classes are Generalization, Realization, Dependency, and Association. Association is divided into general association, Aggregation and Composition. Let’s understand these relationships with examples.

The basic concept

Class Diagram: The Class Diagram is the most common and important Diagram in object-oriented system modeling and is the basis for defining other diagrams. A class diagram is a static model of classes, interfaces, and the static structures and relationships between them.

The three basic components of a class diagram are class name, property, and method.

Generalization: represents the relationship of IS-A, which is the relationship with the maximum coupling between objects, and a subclass inherits all details of its parent class. Use inherited expressions directly in the language. Use solid lines in class diagrams with triangular arrows that point from subclasses to parent classes.

Realization: In a class diagram, the relationship between an interface and an implementation. There’s nothing to talk about. Use dashed lines in the class diagram with triangular arrows that point from the implementation class to the interface.

Dependency: The weakest form of association between objects is temporary. Code generally refers to the local variables, function parameters, return values to establish the call relationship to other objects. A class call relies on methods in the class to perform some of the responsibilities of the class. Use dashed lines with arrows in the class diagram that point from the using class to the dependent class.

Association: A reference relationship between objects, such as a customer class and an order class. This relationship is usually expressed using attributes of the class. Association is divided into general association, aggregation association and combination association. The latter two are analyzed later. Use solid lines in the class diagram with arrows that point from the using class to the associated class. It can be one-way or two-way.

Aggregation: indicates the relationship of HAS-A, which is an unstable inclusion relationship. Stronger than the general correlation, there is a relationship between the whole and the part, and without the whole, the part can also exist alone. For example, the relationship between the company and its employees, the company includes its employees, but if the company goes bankrupt, the employees can still change companies. The class diagram is represented by a hollow diamond that points from the part to the whole.

Composition: indicates the relationship of contains- A, which is a strong containing relationship. The composed class is responsible for the life cycle of the composed class. It’s a stronger convergent relationship. The parts cannot exist without the whole. For example, the relationship between the company and the department, without the company, the department can not exist; The relationship between questions and choices in the questionnaire; Relationship between order and order options. Use solid diamonds in class diagrams that point from part to whole.

Multiplicity: Commonly used for association, aggregation, and combination. That’s how many associated objects there are. Using numbers.. The asterisk (number) indicates. As shown below, a cutover notification can be associated with 0 to N fault tickets.

The difference between aggregation and composition

These two are a little hard to understand, so I’ll focus on them. The difference between aggregation and composition lies in that the aggregation relation is “has-a” relation and the composition relation is “contains-a” relation; Aggregation relation means that the relation between whole and part is weak, but combination is strong. The objects that represent some things in an aggregation relationship are independent of the lifetime of the objects that represent the aggregate things. Once the aggregate objects are deleted, the objects that represent some things are not necessarily deleted. Once you delete a composite object, you also delete an object that represents part of something.

The example analysis

Unicom customers respond to OSS. The system has function modules such as fault list, service opening, resource verification, cutover, service reinsurance, network quality and performance. Now let’s take some of the requirements as examples.

You can see it in the class diagram.

1. Notice is divided into general notice, cutover notice and reinsurance notice. This is inheritance.

2. NoticeService and the implementation class NoticeServiceImpl are the implementation relationships.

3. NoticeServiceImpl is a dependency by referring to a Notice as an argument to the save method. BaseDao completion is also called, which is also a dependency.

4. The connection between the cutover notification and the fault ticket is a general association through an intermediate class (notification circuit).

5. There is an aggregation relationship between the reinsurance notification and the plan repository. Because the plan database can be recorded in advance, there is no necessary connection with the reinsurance notice, and it can exist independently. In the system, it is manually selected from the list. Deleting the reinsurance notice does not affect the plan.

6. There is an aggregation relationship between cutover notifications and requisitions. Similarly, a demand sheet can exist independently of a cutover notification. That is to say, deleting the cutover notification does not affect the requisition.

7. Notifications and replies are combinatorial. Because replies cannot exist independently of notifications. In other words, if a notification is deleted, the corresponding reply must also be deleted in cascade.

After the above analysis, I believe that we have a better understanding of the relationship between classes. If you have any other ideas or good opinions, welcome to clap bricks.

PS: Again: The above class diagram was drawn in Enterprise Architect 7.5. I would like to recommend EA. We can replace Visio and Rose. Visio wasn’t powerful enough, Rose was too heavy. Only EA is suitable.