Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

UML class diagram example diagram

Take a look at this classic UML example diagram (I drew one myself according to the introduction diagram in Baidu Encyclopedia)~ :


Second, UML class diagram example

function graphic describe For example,
Inheritance relationships – spending Solid line + hollow triangle Birds, animals; Bird inherits animal
Implementing an interface • • • spending Dotted line plus hollow triangle Wild geese fly; Wild geese realize flying interface
Implementing an interface — -“ Lollipop notation Donald Duck -○ Speaking Human language; Donald Duck realizes the interface of human speech
correlation —> The solid line cutting head Penguin –> climate; Penguins need to ‘know’ about climate change
dependencies • • • > Dotted line cutting head Animal ••• oxygen; Animals depend on oxygen
The aggregation relationship Left – > Hollow diamond + solid line cutting head Wild goose –> wings; The relationship between parts and whole
Synthesis of relationship In pieces – > Solid diamond + solid line shears Wild geese –> wild geese; A contains B, but B is not part of A

The above table contains the basic diagrams of UML class diagrams.

If you look at the UML class diagram above, you can see most of it. The following details what each part of the class diagram means:


Three, step by step analysis

3.1 class diagram:

Look at the “animal” box. It represents a class. The class diagram is divided into three layers

  • Level 1: Display the name of the class, in italics if it is abstract.
  • Layer 2: Is the class’s properties, usually fields and attributes.
  • Third time: is the method of the class.

The preceding symbols indicate access: “+” for public, “-” for private, and “#” for protected.


3.2 interface:

The “fly”, representing an interface diagram, differs from the class diagram mainly by having the Interface logo at the top.

  • Line 1: Interface name
  • Line 2: interface methods

There is another way to represent an interface, called the lollipop notation, as shown in the figure above, where the “Donald Duck” class implements the “human speaking” interface.


3.3 Implementing Inheritance

Inheritance diagram:Implementation inheritance:

class Bird : Animal{}Copy the code

The inheritance relationship between classes is represented by hollow triangle + solid line.


3.4 Implementation interface:

Implementation diagram:Implementation interface:

class WildGoose: IFly{}Copy the code

3.5 Association Relationship:

Reference the Climate object in the Penguin (Pengui) class.

class Pengui : Bird
{	
	private Climate calimate;
}
Copy the code

Penguins and climate. Penguins need to ‘know’ what’s happening in the climate. They need to ‘understand’ the climate. When a class ‘knows’ about another class, associations can be represented by solid arrows.


3.6 Aggregation Relationship:

Take a look at the two groups of wild geese and wild geese. Wild geese are social animals. Each wild goose belongs to one group, and one group can have more than one wild goose. So they have an aggregation relationship, which is represented by a hollow diamond and a solid arrow.

class WildGooseGroup
{
	private WildGoose[] _WildGooseArray
}
Copy the code

Aggregation represents A weak ‘owning’ relationship, where an OBJECT A can contain an object B, but the object B is not part of an object A.


3.7 Synthesis Relationship:

Here birds and wings are synthetic because they are part and whole, and the life cycles of birds and wings are the same. The composition relationship is represented by solid diamond + solid line arrow.

There are also two numbers at both ends of the line, which is called the cardinality, and this indicates that the class at one end can have several instances. If a class can have an infinite number of instances, it can be denoted by ‘n’. Associative relations, aggregative relations can also have cardinality.

In the Bird class, instantiate the Wing when initialization, they are generated at the same time

class Bird : Animal 
{
	private Wing wing;
	private Bird()
	{
		wing = newWing(); }}Copy the code

Composition is a strong ‘owning’ relationship, which embodies a strict relationship between parts and the whole, with the same life cycle of parts and the whole. Here the bird and its wings are a synthetic relationship.


3.8 Dependencies:

abstract class Animal
{
	public Metabolism(Oxygen oxy, Water water){}}Copy the code

‘Animal’ metabolism needs’ oxygen ‘and’ water ‘to sustain life, so it depends on oxygen and water. This dependency is represented by dotted arrows.


Fourth, UML class diagram drawing tool

More UML tools, commonly used three UML modelers

  1. PowerDesign
  2. Rose
  3. Visio

Other free tools include:

  1. UMLet
  2. ArgoUML
  3. JUDE
  4. BOUml
  5. Visual Paradigm

Recommended free online editing tool: ProcessOn