UML Class Diagrams

4/27/2015
The Unified Modeling Language
Class Diagrams
1
UML 2.0
Unified Modeling Language
 Graphical modeling language for OO systems
 Industry-wide standard
 Based on a metamodel
Build “blueprint” of key system properties
Means of communication
 Among developers
 Between developers and users
Applicable across development phases
Languages
Structure
Behavior
 Class diagram
 Activity diagram
 Component diagram
 UML state machine diagram
 Composite structure diagram
 Use Case Diagram
 Deployment diagram
 Interaction diagrams
 Object diagram
 Package diagram
 Profile diagram
OCL (Object Constraint
Language, Textual)
1
4/27/2015
Association vs. Attribute
What is the difference between Associations
and Attributes?
 Both attributes and associations capture relationships
between classes.
Can an association represent any attribute?
Can an attribute represent any association?
Do they differ only in graphical representation?
Class Diagrams
Created with Papyrus, a
UML tool for Eclipse
Class Diagrams
Abstract Class
2
4/27/2015
Class Diagrams
Interface
Class Diagrams
<<Interface>> is a
stereotype
Class Diagrams
Inheritance
3
4/27/2015
Class Diagrams
Attribute (Java class
field)
Operation (Method in
Java)
Class Diagrams
Association (has name:
assocName)
Class Diagrams
Multiplicity (0 or more)
Multiplicity (exactly 1)
Multiplicity (1 or more)
4
4/27/2015
Class Diagrams
Association End Name
Domain Model of the Library
14
The Library
 Goal: Implement fines on rentables to increase
revenue from delinquent returns
 The library provides members with the opportunity
to check out Publications that include books,
periodicals, DVDs and (old) Video Tapes, each of
which have a different fine associated with it.
 Publications are due two weeks after checkout and
a fine of $1 is incurred for every day late. If this
recurs, the fine is doubled.
 Model this domain using UML. Discuss what patterns
you would use to notify members if they have
reserved a book.
15
5
4/27/2015
From java code to UML Class Diagrams
Can you write the class diagram for the Library
Java code?
16
UML for Strategy (Sample)
17
Strategy
Introduces dependency to
interfaces in the abstract
class.
Each concrete
implementation depends
on an concrete
implementation of the
encapsulated behavior.
Picture from Eric Freeman, Elisabeth Robson, Bert Bates, and Kathy Sierra: Head First Design Patterns, O'Reilly, 2004
6
4/27/2015
Strategy Tradeoffs
<<Interface>>
AlgorithmUser
uses
Algorithm
Algorithm myAlgorithm
Introduces
dependency to
interfaces in the class.
You can change the
algorithm at runtime.
Alg1
doSomething()
Alg2
doSomething()
doSomething()
You need to assign a
concrete
implementation of
Algorithm to the
AlgorithmUser.
Can you add a Strategy to Library?
Can we make the algorithm to compute late
fees pluggable?
Show this in the Class Diagram.
20
Take-Aways
User stories are a starting point for an
architecture
Architecture documents
 Helps in uncovering new requirements
 Helps in managing risks
UML
 Model the domain
21
7
Book.java
1 package edu.ucsd.cse110.library;
2
3 public class Book extends Publication {
4
private String title;
5
public Book(String string) {
6
title=string;
7
}
8
9
protected boolean isLate(int days, String type) {
10
if (type.equals("Teacher")) {
11
//teachers can keep books for 2 weeks
12
return days>14;
13
}
14
if (type.equals("Student")) {
15
if(getCheckoutDate().getMonthValue()>6 &&
16
getCheckoutDate().getMonthValue()<9) {
17
//In summer students can keep books for 1 month
18
return days>30;
19
}
20
//Usually students can keep books for 2 weeks
21
return days>14;
22
}
23
//Everybody else can keep the books for 1 week 24
return days>7;
25
}
26
27
protected double lateFee(int days, String type, double currentFee) {
28
if (type.equals("Teacher") || 29
type.equals("Student") ) {
30
//We charge 1 dollar per late day to students and teachers
31
double assessed = (days‐14)*1;
32
if (type.equals("Student") &&
33
(currentFee+assessed)>10) {
34
//Students are never charged more than 10 dollars per month
35
if (currentFee<10)
36
assessed=10‐currentFee;
37
else
38
assessed=0;
39
}
40
return assessed;
41
}
42
//Everybody else pays 1.5 dollars per late day 43
return (days‐14)*1.5;
44
}
45
46
@Override
47
protected void computeLateFee(int days, Member member) {
48
if (isLate(days, member.getType()))
49
member.applyLateFee(lateFee(days, 50
member.getType(), 51
member.getDueFees()));
52
}
53
54 }
Member.java
1 package edu.ucsd.cse110.library;
2
3 public class Member {
4
private String memberType;
5
private String name;
6
private double fees; 7
public Member(String string, String type) {
8
name=string;
9
memberType = type;
10
}
11
12
public double getDueFees() {
13
return fees;
14
}
15
16
public void applyLateFee(double i) {
17
fees+=i;
18
}
19
20
public String getType() {
21
return memberType;
22
}
23
24 }
Publication.java
1 package edu.ucsd.cse110.library;
2
3 import java.time.LocalDate;
5
6 public abstract class Publication {
7
8
private Member hasBook;
9
private LocalDate checkoutDate;
10
11
public void checkout(Member member, LocalDate checkoutDate2) {
12
hasBook=member;
13
this.checkoutDate = checkoutDate2;
14
}
15
16
public boolean isCheckout() {
17
return checkoutDate!=null;
18
}
19
20
public Member getMember() {
21
return hasBook;
22
}
23
24
public LocalDate getCheckoutDate() {
25
return checkoutDate;
26
}
27
28
public void pubReturn(LocalDate returnDate) {
29
int daysKept = Period.between(checkoutDate, returnDate).getDays();
30
computeLateFee(daysKept, hasBook);
31
hasBook=null;
32
checkoutDate=null;
33
}
34
35
protected abstract void computeLateFee(int days, Member member);
36
37 }