-
Notifications
You must be signed in to change notification settings - Fork 31
UML Notes
UML (Unified Modeling Language) is a general-purpose language to model and visualize designs for a system.
In this class, we will be learning the basics of UML class diagrams to plan out our programming projects.
In previous years, we focused on flowcharting which helped with the direction and flow of the project. This year we will be looking at the UML class diagrams to help us plan our projects. Class diagrams help to ensure encapsulation as well as give us a good overview of the work to complete before starting on our code.
This type of planning is useful in a collaborative environment. Team members have their responsibilities and understanding of how each other class will be set up.
A class diagram is set up into 3 different sections. The class name, the instance variables, and the methods that exist within that object.
| Course |
|---|
| -teacher : String = "Miss Tissism" -courseCode : String |
| +handInWork (work : String) : void +receiveGrade (grade : int) : void |
The above is a basic example of a class diagram that could be used for a course offered at high school. Let's break down each section:
The first section contains the class name Course.
The next section contains the instance variables (teacher and courseCode) as well as the datatypes and default starting values (if necessary).
The last section contains the various methods a Course can have (handInWork() and receiveGrade()). It also contains each of the method's parameter lists. There is one parameter for handInWork which is work of type String, and one parameter for receiveGrade which is grade of type int. Both functions' return type is void.
You will notice most UML diagrams include a + or - symbols before a variable or method. This is to signify public and private respectively. If for some reason a diagram does not include these symbols, then it is assumed they are all public.
In a larger environment, there will be many class diagrams all inter-related in some fashion. This is where a relationship diagram involving the various class diagrams comes in handy.
In the example below, there are two types of connection symbols showing the relationship between the classes. The line with a solid-diamond at one end and a number at the other denotes a Composition. A composition line means that the class numerical end has a dependence on the solid-diamond end. The word(s) in between are used to describe how the two classes are connected. For example, if a Chair doesn't exist, then the vehicle cannot have a Color. This is also known as a "HAS-A" relationship (a chair has a color).
There are only certain numbers that can exist at the end of a composition line. The most common ones are in the table below:
| Number | Meaning |
|---|---|
| 0..1 | No instances, or one instance |
| 1 | Exactly one instance |
| 0.. | Zero or more instances |
| 1..* | One or more instances |
In an inheritance-based environment, your diagrams and relationships will have some slightly different features.
For example, in the detailed diagram below, you will notice a new symbol (the # symbol) in front of either variables or methods within the classes. This symbol means that the variable or method is protected. A protected variable or method is like having a private-to-the-class-chain variable/method. For example, any of the children (or sub) classes can see the variable/method, but nothing outside of the familial relationship can see them.
In a larger environment, there will be many class diagrams all inter-related in some fashion. In the below example, you will notice a new arrow (solid line with an open triangle) which show an "IS A" relationship (a car is a vehicle).

There are multiple types of UML diagrams; however, we will be focusing on only 2 different types. A high-level diagram where you are just looking at the relationships between classes (see below), or a detailed diagram like the ones that are previously shown.
