Design Patterns: UML Class Diagrams (Part 1)

Introduction

UML (Unified Modeling Language) Class Diagrams are a way of drawing diagrams for classes. The aim is to include the following in our diagrams:

  • The names of the classes
  • The attributes of the classes (and whether they are private, protected or public)
  • The member functions of the classes
  • The relationships between the classes

We do not have to include any implementation of functions if we do not need to.

Essentially, we are drawing class prototypes as boxes instead of code. That’s it.

Naming Classes

Classes are to be named using Camel Casing (thisVariableIsInCamelCasing), but the first letter should be uppercase (MyClass).

The reason for this is to be able to distinguish types (classes) from variables, functions or objects. This is for the sake of clarity.

UML Basics

A class is represented using a box with three sub-sections:

  1. The name of the class
  2. The attributes of the class
  3. The member functions of the class

We literally create a box with three sections:

ClassName
Attributes:
public
protected
private
Functions:
public
protected
private

An example is below. Don’t worry we’ll get to everything now.

Person
+name: string
+age: int
+height: double
-weight: double
+getWeight(): double
+setWeight(): void
-calculateBMI(): double

UML Types

Writing variables is backwards to most programming languages in UML. Instead of writing the type before the name of the variable, we write it after, separated by a colon :

We use symbols to represent whether the variable is public, protected or private (three Ps):

  • Public: + positive, because you are always allowed to access it
  • Protected: # hash, because you can sometimes access it (combination between a + and a -)
  • Private: - negative, because you aren’t allowed to access it

A brief recap on how these work: it’s best to think of them as a hierarchy, where private is the least accessible and public is the most.

  • Private: the member isn’t accessible from anywhere outside of the class. This means that only functions inside the class can use it. When the class is inherited from (it has a child- or subclass) these attributes do not exist on the child class.
  • Protected: these work exactly the same as private, but subclasses do have these members (you only use these if you need to have subclasses)
  • Public: these are accessible from outside the class, and also exist on subclasses.

 


Accessible from outside?
Exists when extended?
private
No
No
protected
No
Yes
public
Yes
Yes

 

Examples and their code equivalents:

Code
UML Equivalent
public string name;
+name: string
public int age;
+age: int
protected double weight;
#weight: double
protected bool isObese;
#isObese: boolean
private Character spouse;
-spouse: Character
private int numberOfChildren;
-numberOfChildren: int

I recommend that you practice these a bit. Understanding UML makes life a lot easier later.

Writing Functions

We write functions in the same way. They can be a little bit complicated, but just break it down into its smaller parts and it’s easy.

An example function:

Code: protected double calculateBodyMassIndex(double weight, double height);

UML: #calculateBodyMassIndex(weight: double, height: double) : double

Another example, possibly clearer:

Code: public int getLengthOfString(string theString);

UML: +getLengthOfString(theString: string) : int

Examples

 

Person
+name: string
+age: int
+height: double
-weight: double
+getWeight(): double
+setWeight(): void
-calculateBMI(): double

 

Car
+name: string
+year: int
+maxSpeed: double
#acceleration: double
#engine: string
-weight: double
-cylinders: int
+getZeroToHundred() : double
-calculateHorsePower() : double

 

ShopListing
+name: string
+id: int
+description: string
+price: double
#shippingCostPerKilometre: double
+calculateCostOfShipping(distance: double) : double
+addToCart()

 

Article
+id: int
+title: string
+author: string
+datePublished: string
+category: int
-contents: string
-authorId: int
-isDraft: boolean
+getContents() : string

 

 

BankAccount
+accountNumber: int
-accountHolder: string
-accountBalance: double = 0.0
-interestRate: double = 6.0
+addToBalance(amount: double) : void
+drawMoney(amount: double) : double

 

Student
+studentNumber: int
+name: string
+picture: Image
-modules: Module[0..*]
-accountBalance: double
+payFees(amount: double) : void
+getModules() : Module[0..*]
+setPicture(image: Picture) : void

 

Copyright © Matthew Evans 2017