preface

As the first in this series on design patterns, I’m going to share how to understand UML class diagrams. By the end of this article, you’ll be able to understand the relationships between classes in a class diagram and what lines and arrows mean. At the same time, we will match the meaning of the class diagram to the final code implementation; With this knowledge, there is no problem looking at the design pattern structure diagram in the later sections.

Let’s start with an example

Below is a class diagram I drew using the tool StarUML, the Mac version download address

  • The number is italicized to indicate that it is an abstract class
  • Digital has two inheritance classes, phone and computer, and the relationship between them is the realization relationship, represented by a dotted line with a hollow arrow
  • There is also an inheritance relationship between iPhone and mobile phone. The relationship between them is a generalization relationship, represented by a solid line with a hollow arrow
  • The phone is composed of a screen, a motherboard, and so on. The relationship between them is a combinative relationship, represented by a solid line with a solid diamond
  • Programmers have to use computers to go to work, and the relationship between them is a dependency relationship, indicated by dotted lines with arrows
  • Programmers and companies are aggregated, represented by solid lines with hollow diamonds
  • Programmers generally have their own cards, and the relationship between them is an association, represented by a solid line

I have highlighted the six relationships of classes in the examples above and in the figures above. Let’s talk about these six relationships in detail

Relationships between classes

The inheritance structure of a class is expressed in UML as implementation and generalization

The inheritance relationship is IS-A. If two objects can be represented by IS-A, it is an inheritance relationship

Realization

“Digital” is an abstract concept that cannot be used to define objects directly in real life. It must specify subclasses, such as mobile phones and computers. The “digital” class can be represented as an interface or abstract class in Java or as an abstract class in C++.

Code implementation: The implementation relationship is represented by inheriting abstract classes.

Generalization.

Mobile phone is realized in reality and can be used to define specific objects. IPhone is a subclass of mobile phone, and the relationship between mobile phone and iPhone is a generalization relationship.

Code implementation: The generalization relationship is represented by inheriting non-abstract classes.

Aggregation

Aggregation means that there is a weak relationship between the whole and the parts of the two objects, and the life cycle is not synchronized. The life cycle of the parts can go beyond the whole, such as the programmer and the company. The company goes bankrupt, but the programmer is still there and can go to other companies.

Code implementation: the aggregation relationship is implemented in the form of member variables, but the assignment time of member variables is in the class method, the code is as follows:

// The first is to set the instance of the subclass directly in the set method
class Company {
    private Programmer programmer;

    public void setProgrammer(JavaProgrammer javaProgrammer) { programmer = javaProgrammer; }}// The second way is to define an initial value directly in the set method
class Company {
    private Programmer programmer;

    public void setProgrammer(a) {
         programmer = newJavaProgrammer(); }}Copy the code

Composition

Combination means that there is a strong relationship between the whole and the parts of two objects with the same life cycle. The life cycle of a part cannot exceed the whole, or the whole in a combination cannot lack a part. Just like a phone can’t live without a screen and a motherboard.

Code implementation: the combinative relation is implemented in the form of member variables, but the assignment time of member variables is in the class constructor, the code is as follows:

// A mobile phone consists of a screen
class Phone {
    private Screen screen;

  	// Create the screen while creating the phone
    public Phone(a) {
        screen = newScreen(); }}Copy the code

Association

Association refers to the fixed correspondence between two different objects. It is a static relationship and has nothing to do with the running state.

Code implementation: The association is also implemented in the form of a member variable, but the assignment time of the member variable is when the variable is declared, the code is as follows:

// All programmers have jobs
class Programmer {
    private Card card = new Card();
}
Copy the code

Dependency

A dependency represents a relationship between an object and another object at run time. Unlike an association, it is a temporary relationship that usually occurs at run time and may change as the run time changes. For example, programmers use computers when they work. Programmers depend on computers.

Code implementation: The dependency relationship can be mainly realized by method parameters, method local variables, static methods in three ways, method parameter code is as follows:

class Programmer {
  	// Programmers need computers to work
    public void work(Computer computer) { computer.work(); }}Copy the code

conclusion