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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
UML (Unified Model Language)
|
| 2 |
+
to document object-oriented programs
|
| 3 |
+
|
| 4 |
+
It can be used to graphically depict the BASIC STRUCTURES of object-oriented programming, such as CLASSES and their INHERITANCE relationships.
|
| 5 |
+
These diagrams are usually referred to as CLASS DIAGRAMS.
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
Class
|
| 15 |
+
In UML, a class is represented by a frame consisting of 3 parts.
|
| 16 |
+
At the top is the class NAME,
|
| 17 |
+
in the middle are the class ATTRIBUTES, and
|
| 18 |
+
at the bottom are the class METHODS.
|
| 19 |
+
|
| 20 |
+
ATTRIBUTES are presented in the form
|
| 21 |
+
name: type
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
A + or - is written in front of the attribute depending on whether the attribute is 'public' or 'private'.
|
| 25 |
+
The sign for a 'protected' attribute is (usually) #.
|
| 26 |
+
|
| 27 |
+
So for example a protected, double type attribute balance would be marked:
|
| 28 |
+
#balance: double
|
| 29 |
+
|
| 30 |
+
...and a private Student type attribute tutor:
|
| 31 |
+
-tutor: Student
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
METHODS are represented in a similar form, but also the PARAMS of the methods are announced in the diagram.
|
| 38 |
+
The 'method type' is reported after the parameter list.
|
| 39 |
+
|
| 40 |
+
So, for example, the set method 'setName' could be marked as follows:
|
| 41 |
+
+setName(name: String): void
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
=================================================
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
Let's look at the class 'Exercise' as an example:
|
| 52 |
+
|
| 53 |
+
class Exercise {
|
| 54 |
+
// PRIVATE ATTRIBUTES
|
| 55 |
+
private String correctAnswer;
|
| 56 |
+
private String exerciseDescription;
|
| 57 |
+
private double points;
|
| 58 |
+
|
| 59 |
+
// CONSTRUCTOR
|
| 60 |
+
public Exercise(String correctAnswer, String exerciseDescription, double points) {
|
| 61 |
+
this.correctAnswer = correctAnswer;
|
| 62 |
+
this.exerciseDescription = exerciseDescription;
|
| 63 |
+
this.points = points;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
public String getExerciseDescription() {
|
| 67 |
+
return exerciseDescription;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
public double getPoints() {
|
| 71 |
+
return points;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
public void setPoints(double points) {
|
| 75 |
+
this.points = points;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
public void checkAnswer(String answer) {
|
| 79 |
+
if (answer.equals(correctAnswer)) {
|
| 80 |
+
points = 10;
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
The UML diagram drawn from the class would look like this:
|
| 87 |
+
https://ville.utu.fi/APP/connector/0/219/source/9c79f234-86e5-43c5-821a-eaee0895bdeb.png
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
Exercise
|
| 91 |
+
-correctAnswer: String
|
| 92 |
+
-exercise Description: String
|
| 93 |
+
-points: double
|
| 94 |
+
|
| 95 |
+
+Exercise (correctAnswer: String, exercise Description: String, points: double)
|
| 96 |
+
+getExerciseDescription(): String
|
| 97 |
+
+getPoints(): String
|
| 98 |
+
+setPoints(points: double): void
|
| 99 |
+
+checkAnswer(answer: string): void
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
====================================
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
Modeling Class Hierarchies
|
| 111 |
+
|
| 112 |
+
Inheritance is marked with an ARROW that STARTS FROM the INHERITING CLASS.
|
| 113 |
+
The head of the arrow is a triangle.
|
| 114 |
+
If, for example, we have a class 'Teacher' that inherits from the class 'Person', the diagram could look like this:
|
| 115 |
+
https://ville.utu.fi/APP/connector/0/227/source/bf884576-16f4-4ef0-a9e5-a4e69cf1faa3.png
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
Person
|
| 120 |
+
+email: String
|
| 121 |
+
-name: String
|
| 122 |
+
+Person(email: String, name: String)
|
| 123 |
+
+getName(): String
|
| 124 |
+
+setName(): String
|
| 125 |
+
|
| 126 |
+
^
|
| 127 |
+
-
|
| 128 |
+
|
|
| 129 |
+
|
|
| 130 |
+
|
|
| 131 |
+
|
|
| 132 |
+
|
|
| 133 |
+
|
| 134 |
+
Teacher
|
| 135 |
+
-teachingSubject: String
|
| 136 |
+
+Teacher(email: String, name: String, teachingSubject: String)
|
| 137 |
+
+getTeaching Subject(): String
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
Interface classes are marked by writing the word <interface> above the class name.
|
| 147 |
+
The implementation of the 'interface' is marked with an arrow similar to inheritance.
|
| 148 |
+
|
| 149 |
+
For example, the 'interface' class 'Priced' and its implementing class 'Product':
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
interface Priced {
|
| 154 |
+
double getPrice();
|
| 155 |
+
void setPrice(double price);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
class Product implements Priced {
|
| 160 |
+
|
| 161 |
+
private String name;
|
| 162 |
+
private double price;
|
| 163 |
+
private String code;
|
| 164 |
+
|
| 165 |
+
public Product(String name, double price, String code) {
|
| 166 |
+
this.name = name;
|
| 167 |
+
this.price = price;
|
| 168 |
+
this.code = code;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
@Override
|
| 172 |
+
public double getPrice() {
|
| 173 |
+
return price;
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
@Override
|
| 177 |
+
public void setPrice(double price) {
|
| 178 |
+
this.price = price;
|
| 179 |
+
}
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
...could be represented with a diagram like this:
|
| 184 |
+
https://ville.utu.fi/APP/connector/0/234/source/9b1af723-9438-4cb5-96bc-d20e586f2239.png
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
<interface>
|
| 189 |
+
Priced
|
| 190 |
+
-getPrice(): double
|
| 191 |
+
+setPrice(price: double): void
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
^
|
| 195 |
+
-
|
| 196 |
+
|
|
| 197 |
+
|
|
| 198 |
+
|
|
| 199 |
+
|
|
| 200 |
+
|
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
Product
|
| 204 |
+
-code: String
|
| 205 |
+
-name: String
|
| 206 |
+
-price: double
|
| 207 |
+
+Product(code: String, name: String, price: double)
|
| 208 |
+
+getPrice(): double
|
| 209 |
+
+setPrice(price: double): void
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
====================================
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
More Information on Class Diagrams
|
| 228 |
+
|
| 229 |
+
There is plenty of additional information about UML and class diagrams on the internet.
|
| 230 |
+
|
| 231 |
+
Diagrams can be drawn with almost any drawing program.
|
| 232 |
+
A good free environment for drawing diagrams is DIA, for example:
|
| 233 |
+
http://dia-installer.de/
|
| 234 |
+
|
| 235 |
+
The online draw.io is also well suited for drawing UML diagrams:
|
| 236 |
+
https://app.diagrams.net/
|
| 237 |
+
|
| 238 |
+
There are also various plugins for Eclipse and other editors
|
| 239 |
+
that can automatically generate a UML diagram from your code (or even code from a UML diagram).
|
| 240 |
+
|
| 241 |
+
|