UML 1 Class Diagrams
UML 1 Class Diagrams
Suggested reading:
Practical UML: A hands on introduction for developers
http://dn.codegear.com/article/31863
UML Distilled Ch. 3, by M. Fowler
1
How do people
draw / write down
software architectures?
Design phase
• design: specifying the structure of how a software
system will be written and function, without actually
writing the complete implementation
It is easier to understand any concept if you are shown a small example of such a
thing. Object diagrams are useful in this way, as they’re simpler than class diagrams
UML class diagrams
• UML class diagram: a picture of
– the classes in an OO system
– their fields and methods
– connections between the classes
• that interact with, or inherit from, each other
• Not represented in a UML class diagram:
– details of how the classes interact with each other
– algorithm details; how a particular behavior is
implemented. E.g. the methods can include
+ calcGPA( ) : int
which does not show how GPA will be calculated
How do we design classes?
• class identification from project spec / requirements
– nouns are potential classes, objects, fields
– verbs are potential methods or responsibilities of a class
• CRC card exercises
– write down classes' names on index cards
– next to each class, list the following:
• responsibilities: jobs/services it will provide; short verb phrases
• collaborators: other classes that are sent messages by this class
(asymmetric)
• UML diagrams
– class diagrams (today)
– sequence diagrams
– ...
Peeping ahead (details next term):
An abstract class is a class where one or more
methods have no code body yet
Eg. abstract class Pet: methods like age will have a code
body (today – dob; for all kinds of pet), but makeSound
will have no code body yet (depends what kind of pet!)
• attributes (optional)
– should include all fields of the object
an abstract
class
a (ordinary)
class
Class attributes (fields, instance variables)
– visibility name : type [count] = default_value
– visibility: + public
# protected
- private
~ package (default)
/ derived
– attribute example:
- balance : double = 0.00
– visibility: + public
# protected
- private
~ package (default)
– method example:
+ distance(p1: Point, p2: Point): double
Comments
• represented as a folded note, attached to the
appropriate class/method/etc by a dashed line
Relationships between classes
• generalization: an inheritance relationship
– inheritance between classes
– Inheritance between interfaces
Generalisation/inheritance is used when you have a general kind of object, and you
also have different specialized kinds of that object.
Here, a company has Employees, but there are 9 different kinds of Employee, which
have a lot in common but a few differences.
E.g. all Employees have an ID, phone, email etc but HourlyEmployees have
hourlyPay & overtimePay, while SalariedEmployees have monthlyPay & xmasBonus.
TechnicalStaff & Executives are different kinds
7-19 of SalariedEmployees, etc.
Copyright © 2017 Pearson Ltd. All rights reserved.
Generalization (inheritance) relationships
• hierarchies drawn top-down
• arrows point upward to parent
• line/arrow styles indicate whether parent is
a(n):
– class:
solid line, black arrow
– abstract class:
solid line, white arrow
– interface:
dashed line, white arrow
– abstract class:
solid line, white arrowhead
– interface:
dashed line, white arrowhead
RectangularShape is an abstract
class because of the solid line with
white arrowhead, & because its name
is in italics
Associational relationships
• associational (usage) relationships
1. multiplicity (how many are used)
* ⇒ 0, 1, or more
1 ⇒ 1 exactly
2..4 ⇒ between 2 and 4, inclusive
3..* ⇒ 3 or more (also written as “3..”)
2. name (what relationship the objects have)
3. navigability (direction)
Association types Car
1
aggregation
• aggregation: “is part of” 1
Engine
– symbolized by a clear white diamond
composition
– stronger version of aggregation
1
– the parts live and die with the whole
*
– symbolized by a black diamond
Page
CheeseBurger and
Only a Packaging are related, but A cheeseburger must comprise
relationship one can exist without the (be made of) a bun, a patty and
between classes other cheese; if the cheeseburger goes,
then the bun, patty & cheese go
https://www.visual-paradigm.com/guide/uml-unified-modeling-
language/uml-aggregation-vs-composition/
https://sparxsystems.com/resour
ces/tutorials/uml2/class-
diagram.html
Multiplicity of associations
one-to-one
each student must carry exactly one ID card
www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-class-diagram-tutorial/
Class diagram example: video store
Multiplicity
Customer Simple
1
Class Aggregation
Composition Simple
Generalization Association
Checkout Screen
DVD Movie VHS Movie Video Game
https://www.edrawmax.
com/article/uml-class-
diagram-explained.html
Tools for creating UML diagrams
• Violet (free)
– http://horstmann.com/violet/
• Rational Rose
– http://www.rational.com/
1
UML Class Diagram: Exercise
What classes and associations to use here?
We need a software system for a consortium of 4 banks. Customers can
open any number of accounts at each bank, and can be customers of
any number of the 4 banks. At every bank, there is only 1 customer that
holds that account. There are 2 types of account, Savings accounts and
Loan accounts. All accounts have a history of their balance as it
changes over time; loan accounts also have a minimum (monthly
payment) and a terms value (number of months within which they must
be fully paid back) . Customers can perform 3 kinds of transaction on
their accounts: deposits, withdrawals and transfers (of money from 1
account to another). Customers can have any number of credit cards,
and each credit card belongs to only one customer.