When we read open source projects, we always hope to effectively sort out the relationship between the various classes in the project. So is there any corresponding tool that can efficiently and concisely express the clear relationship between the classes? A UML class diagram is a tool or method that can help us solve this problem.

1 What is UML

The Unified Modeling Language (UML) is a generic, third-generation Modeling and specification Language. UML is an open method for describing, visualizing, building, and writing the artifacts of an ongoing, object-oriented, software-intensive system.

UML models and graphics

UML is divided into models and graphics. It is important to distinguish between UML models and UML diagrams. UML diagrams (including use case diagrams, collaboration diagrams, activity diagrams, sequence diagrams, deployment diagrams, component diagrams, class diagrams, and state diagrams) are a graphical representation of the information in the model, but UML models exist independently of UML diagrams.

There are three main models in UML system development:

  • Functional model: Shows the functions of the system from the user’s perspective, including use case diagrams.
  • Object model: Using objects, attributes, operations, associations and other concepts to show the structure and foundation of the system, including category diagram, object diagram.
  • Dynamic models: Show the internal behavior of the system. Including sequence diagram, activity diagram, state diagram.

A total of 14 ICONS are defined in UML2.2.

Structure Diagrams emphasize systematic modeling:

  • Static Diagram: includes the class diagram, object diagram, and package diagram
  • Implementation Diagram: includes component diagram, deployment diagram
  • profile
  • Composite structure drawing

Behavior Diagrams emphasize events triggered in a system model

  • Activity diagrams
  • State diagram
  • Use case diagram

Interaction Diagrams, which belong to a subset of behavior diagrams, emphasize data flow in system models

  • Communication diagrams
  • Interaction overview diagram
  • Sequence diagram
  • Time to figure

2 FUNCTIONS of UML class diagrams

UML represents a set of best engineering practices that have been proven to be effective in modeling large-scale, complex systems, especially at the software architecture level.

This time, we mainly introduce the class diagram. In order to parse the system structure and architecture level of the project, it can help us understand the relationship between the classes in the project succinct and clear.

The functions of class diagram are as follows: (1) : In software engineering, class diagram is a static structure diagram, describing the collection of system classes, class attributes and the relationship between classes, can simplify people’s understanding of the system; (2) : Class diagram is an important product of system analysis and design, and an important model of system coding and testing.

3 types of graph formats

In UML class diagrams, classes are represented by rectangles with dividing lines that contain class names, fields, and methods.

Take chestnuts. An Animal class that contains the name,age,state,isPet attributes and the name related methods.

class Animal: NSObject {

	public var name: String?
	internal var isPet: Bool?
	fileprivate var state: String?
	private var age: Int? = 0

	override init() {
    	self.name = "no name"
    	self.age = 0
    	self.isPet = true
    	self.state = "dead"
	}

	public func getName() -> String {
    	return self.name!
	}

	internal func setName(name: String?) {
    	self.name = name
	}
}
Copy the code

Corresponding UML class diagram:

  • Class name: bold, or italic if the class is abstract!
  • Properties:

Visibility name: type [= default]

Visibility is generally public, private and protected, which are respectively represented by +, – and # in the class diagram. In Swift, there is no visible control corresponding to protected, so internal is used and corresponding to #. Name is the name of the attribute; The type is data type. Default values such as the age variable are 0 by default.

  • Methods:

Visibility name (parameter list parameter 1, parameter 2) : Return type

As described in the preceding name expression, the name is the method name, and the parameter list is optional. If multiple parameters are used, the parameters are separated by commas (,). The return value is also optional. The return value types can be basic data types, user-defined types, and void. If it is a constructor, there is no return type!

Expression of relationships between classes

The relationship between classes in the class diagram mainly consists of six types: inheritance, implementation, dependency, association, aggregation, and combination. The representation is shown as follows:

(1) Generalization/ Generalization

Inheritance relationship, also called generalization relationship, refers to the ability of a class (called a subclass, subinterface) to inherit the functionality of another class (called a parent class, parent interface) and add its own new functionality. Inheritance is the most common relationship between classes or interfaces.

Inheritance is represented by a solid hollow arrow pointing from a child class to the superclass.

Let’s write two subclasses, Fish and Cat from Animal.

class Fish: Animal {
    
    public var fishType: String?
    func swim() {
    }
}

class Cat: Animal {
    public var hasFeet: Bool?
    func playToy(doll:Doll) {
        doll.toyMoved()
    }
}
Copy the code

(2) Implements

A class that implements the function of an interface. Implementation is the most common relationship between a class and an interface; In Java this relationship is explicitly identified by the keyword implements, but in iOS I think of it as an implementation of a broker.

Write a Doll class called Doll, which follows the ToyAction protocol and implements the method of moving the toy.

protocol ToyAction { func toyMoved() -> Void } class Doll: NSObject,ToyAction { public var body: Body? public var cloth: Cloth? Func toyMoved() {// Doll doll action concrete implementation}}Copy the code

(3) Dependency

It can be simply understood that one class A uses another class B, and this usage relationship is contingent, temporary and very weak, but the change of class B will affect A. For example, if someone wants to cross a river and needs to borrow a boat, the relationship between man and boat is dependence. At the code level, class B is used as A parameter by class A in A method.

In our code above, the arguments in Cat’s playToy method refer to Doll, so they are dependencies.

(4) Association

It represents a strong semantically dependent relationship between two classes or between a class and an interface, such as me and my friend. This relationship is stronger than dependence, there is no contingency of dependence, the relationship is not temporary, is generally long-term, and the relationship between the two sides is generally equal, association can be one-way, two-way; At the code level, associative class B appears in associative class A in the form of class attribute, or it may be that associative class A refers to A global variable of type associated class B.

Write a Person class that owns a pet cat, and the relationship between them is an association.

class Head: NSObject {
    
}

class Person: NSObject {
    public var pet: Cat?
    public var head: Head?
}
Copy the code

(5) Aggregation

Aggregation is a special case of association relationship. It reflects the relationship between the whole and the part and the ownership, namely the relationship of has-A. At this time, the whole and the part are separable, they can have their own life cycle, and the part can belong to or be shared by multiple whole objects. For example, the relationship between computer and CPU, company and employee; At the code level, it is consistent with the relation, which can only be distinguished from the semantic level.

class Cloth: NSObject {
    
}

class Body: NSObject {
    
}
Copy the code

In the code above Doll consists of a Body and a Cloth, and Doll will still exist without the Cloth.

(6) Composition

Combination is also a special case of association relationship, which embodies a relationship of contains-a, which is stronger than aggregation, also known as strong aggregation; He also embodies the relationship between the whole and the parts, but at this point the whole and the parts are inseparable, and the end of the life cycle of the whole means the end of the life cycle of the parts; Like you and your brain; At the code level, it is consistent with the relation, which can only be distinguished from the semantic level.

The Person in the above code has the Head, and the whole and the part are indivisible.

Finally, look at the overall relationship in this example:

In fact, after understanding it, we found that it was still very simple. After learning it, we could put it into practice. For example, the class diagram of a simple third-party library is taken as an example.

​ ​

​ ​