UML Class Diagram
UML Class Diagram
BankAccount
Class Diagrams methods & attributes
BankAccount BankAccount
balance deposit(amount)
owner withdraw(amount)
getBalance( )
BankAccount
balance
owner
citizen_id
deposit(amount)
withdraw(amount)
getBalance( )
Class Diagram with data types
Class diagram can show data types & visibility
BankAccount
balance: double
accountId: string
deposit(amount: double)
withdraw(amount: double): boolean
getBalance( ): double
Visibility of Members
+ Public. Any code can access
# Protected. Only this class and subclasses can access
~ Package. Only classes in same package
- Private. Only this class can access.
BankAccount
-balance: double
#accountId: string
+deposit(amount: double): void
+withdraw(amount: double): boolean
+getBalance( ): double
Visibility Prefixes
+ means public
Visible everywhere
– means private
Visible only in the class in which it is defined
# means protected
Visible either within the class in which it is defined or
within subclasses of that class
~ means package or default visibility
visible to other classes in the same package
Notation for Constructors
BankAccount BankAccount
-balance: double -balance: double
<<constructor>> or +BankAccount(owner)
+BankAccount(owner) +deposit(amount)
+deposit(amount) . . .
. . .
Python:
BankAccount
-balance: double
+__init__(self,owner)
+deposit(amount)
. . .
Static Members
Use underscore to show static (class) attributes or
methods.
Example: BankAccount has a static nextAccountId
attribute.
BankAccount
-nextAccountId: long static attribute
-balance: double
-id: long
+create_account(accountId) static method
+getBalance( ): double
. . .
Python: class members
class MyClass:
pub_date: date # a class attribute
def __init__(self, myname: str):
self.name = myname
def f(x): # an instance method
print("my name is ", x.name)
@classmethod
def g(cls, *args):
"""can access class members, but not
instance members"""
print("Class method")
@staticmethod
def h(*args):
print("a static method")
Practice: Draw the class diagram
class Student:
ID_PATTERN = "[1-9]\\d{9}" # regular expression
def __init__(self, id, name):
if not re.fullmatch(ID_PATTERN, id):
raise ValueError("Invalid student id")
self.id = id
self.name = name
def get_name(self):
return self.name
@classmethod
def get_year(cls, id: int):
# this only works for 10-digit KU ids
return 2500 + (self.id//100000000)
Showing Multiplicity in UML
var: Type[*] means var is a collection, including array.
BankAccount
-owner id attribute
-balance not shown
-accountNumber
+deposit( )
+withdraw( )
Showing Relationships in UML
Includes "association".
GameConsole Game
class GameConsole:
# the play method depends on Game.
def play(game: Game):
(width,height) = game.get_size()
More Dependency
Main depends on (uses) Game and GameConsole
<<creates>> GameConsole
Main +play(game: Game)
<<creates>>
Game
class Main:
@classmethod
def run(cls):
game = Game(600,800)
ui = GameConsole()
ui.play( game )
Dependency Example
A Student uses the Registrar to enroll in a Course, but he
doesn't save a reference (association) to the Registrar.
class Student:
# NO Registrar attribute!
class GameConsole:
// Java
public class CardDeck {
private Card[] cards;
public CardDeck() {
cards = new Card[52];
...
}
Association with Variable Multiplicity
A MailBox may contain 0 or more mail messages.
* = any number (0 or more)
1..n = 1 to n MailBox * MailMessage
n = exactly n
class MailBox:
def __init__(self):
self.messages = []
Person * Company
employer: Company employees: Person[*]
Question has a choice_set attribute (added by
Django) with zero or more Choices.
A Choice has only 1 Question.
Don't show the "id" attribute.
Aggregation: whole-parts relationship
One class "collects" or "contains" objects of another.
A Mail Box stores (collects) Mail Messages
MailBox * MailMessage
ChessBoard Square
64
class Student:
# student uniquely owns his email addresses
email_address: list
Inheritance
T T is a type
parameter
Summary of relationships
B A depends on or uses B
A
A B A has an association to a B
A B A is a subclass of B
A implements interface B
A B
Bogus relationships
A B Nonsense (aggregation is a
form of association)
UML is for Communication
To communicate clearly, use the correct notation.
wrong
Student Person
wrong <<interface>>
Comparable
UML-diagrams.org.
Detailed, concise explanation of UML syntax.
https://www.uml-diagrams.org/class-diagrams-
overview.html
https://www.uml-diagrams.org/property.html
Video
UML Class Diagram, https://youtu.be/UI6lqHOVHic.
Video by LucidChart, online diagramming software.
In the "Animal" example, age should be a
method, not an attribute. Why?
What should they use instead of age as
attribute?