This article draws class diagrams based on Sublime’s PlantUML plug-in. To learn about plug-in installation, click on Sublime Install PlantUML plug-in

UML representation of a class

Representing a class in UML consists of three main parts. Class name, attribute, method. The access modifiers for properties and methods use -, #, + to denote private, protected, and public.

As shown, class A has A private property, A protected constructor, and A public method.

@startuml class A{-string field + A() # void method()} note right: this is A test class @endUMLCopy the code

Type of relationship

In object-oriented languages, there are many kinds of class relationships, which can be summarized into three categories: generalization, dependency and association.

generalization

Generalization refers to the relationship between the parent class and the child class, and represents the relationship between IS – A. If the parent is an abstract class or an ordinary class, the relationship is called inheritance. If the parent class is an interface, the relationship is called an implementation. In UML, inheritance and implementation are represented by different tags.

inheritance

Inheritance PlantUML use – | > said. Abstract representations of solid lines and triangles are inherited from whoever they point to.

@ startuml A abstract class B 'inheritance B A - | > @ enduml BCopy the code

implementation

PlantUML with.. | > said relations. Abstract representation of virtual and triangle, which point to, that is, who is implemented.

@startuml class A interface C 'a implementation C A.. |> C @endumlCopy the code

Rely on

The weakest association between classes. It is often used to use class B objects as arguments, local variables, or calls to class B static methods in class A methods.

PlantUML with.. > indicates a dependency. Dashed lines and abstract representations of arrows depend on who they point to.

@startuml class A class B 'a depend on B A.. > B @endumlCopy the code

associated

Association relationships, that is, reference relationships between objects. Class attributes are often used.

One-way link

Class B, as an attribute of class A, indicates that class A is associated with class B. PlantUML denotes one-way association with –>. An abstract representation of a solid line and an arrow.

@startuml class A{-b B} class B 'a associated B A -> b@endumlCopy the code

Bidirectional association

Class B is an attribute of class A and class A is also an attribute of class B, indicating bidirectional association. PlantUML uses — to denote bidirectional association. Or use <–>.

@startuml class A{-b B} class B{-a A} 'A associated B A -- B @endumlCopy the code

Since the correlation

Class A is associated with class A itself. This is common in the singleton pattern.

@startuml class A{-a A} 'A associated A A -> A @endumlCopy the code

The aggregation

On the basis of association relation, aggregation relation, strong association relation, has- A relation is extended. The relationship between the whole and the parts. The parts are independent of the whole and can exist independently. Often used with member variables.

Such as; The relationship between a car and a tire, which can be sold as a separate commodity.

PlantUML uses O — for aggregation. Abstract representations of solid lines and hollow diamonds, indicating who is the whole.

@startuml class Car{-list <Wheel> wheels} class Wheel 'Car associated Wheel Car "1" o-- "4" Wheel @endumlCopy the code

The numbers 1 and 4 in the figure also represent a one-to-many association. Same thing for N.

combination

On the basis of association relation, another association relation, combination relation, is extended, indicating contains- A relation. The relationship between the whole and the part, the part depends on the whole, cannot exist independently. Often used with member variables.

For example, the relationship between body and movement.

PlantUML uses *– to indicate aggregation. Abstract representations of solid lines and solid diamonds, pointing to whoever is the whole.

@startuml class Body{-list <Action> Actions} class Action 'Body Associated with Action Body "1" *-- "N" Action @endumlCopy the code

PlantUML typesetting

Compare this to other UML software or plug-ins. The advantage of PlantUML is that it stores text files for easy teamwork and highly customizable dependencies. However, the biggest disadvantage is that the typesetting is automatically generated by plug-ins, and the typesetting effect is not satisfactory. Therefore, PlantUML provides four keywords up down left right. Specifies the relative relationships between classes.

default

@startuml

class A1
class B1

A1 --> B1

class A2
class B2
A2 <-- B2

@enduml
Copy the code

Arrow to the left points to the object on; The arrow pointing to the right is under the object.

up

@startuml

class A1
class B1

A1 -up-> B1

class A2
class B2
A2 <-up- B2

@enduml
Copy the code

When up is used, the object being pointed to is on.

down

@startuml

class A1
class B1

A1 -down-> B1

class A2
class B2
A2 <-down- B2

@enduml
Copy the code

When down is used, the object to be pointed to is down.

left

@startuml

class A1
class B1

A1 -left-> B1

class A2
class B2
A2 <-left- B2

@enduml
Copy the code

With left, the object being pointed to is to the left.

right

@startuml

class A1
class B1

A1 -right-> B1

class A2
class B2
A2 <-right- B2

@enduml
Copy the code

When right is used, the object being pointed to is to the right.

conclusion

Drawing class diagrams is just one of the functions of PlantUML. You can also use it to draw use-case diagrams, sequence diagrams, and activity diagrams. For more usage, please follow this blog or visit our website.

Here’s the AD: Flueky’s tech site