UML Class Diagrams Sequence Diagrams

Download Report

Transcript UML Class Diagrams Sequence Diagrams

UML Class Diagrams
Sequence Diagrams
CSE230
Dae-Kyoo Kim
Agenda

Class Diagrams




Symbology
Basic Modeling
Intermediate Modeling
Sequence Diagrams

Symbology
Class Diagrams

A class diagram shows:

Classes






Attributes
Methods
Interfaces
Collaborations
Relationships: Dependency, Generalization,
Association
A class diagram is a STATIC view of system
Basic Class Diagrams
Class Name
Window
Attributes
size: Size
visibility: boolean
Operations
display()
hide()
Basic Class Diagrams
+
#
-
public
protected
private
/
$
derived
static
Class Scope Variable
Abstract
<<abstract>>
<<constructor>>
<<query>>
<<update>>
…
Visibility Attribute Name [Multiplicity]:Type = Initial Value
Visibility Method Name (Parameter List) : Return-List
Attribute Example
(Java implementation)
Note
+ author: String = “unknown”
+ text : String
- total : long = 0
public class Note
{
public String author = “unknown”;
public String text;
private static long total = 0;
...
}
Operation Example
(Java implementation)
Figure
- size: Size
- pos : Position
+ move(pos : Position)
+ getFigureCount() : long
public class Figure
{
private Size size;
private Position pos;
private static long figureCount = 0;
public void move(Position pos) { ... }
public static long
getFigureCount() { return figureCount; }
...
}
Basic Class Diagrams
Class A
Superclass
Class with
parts
Class with
parts
Interface
Subclass
Assembly
Class
Assembly
Class
Concrete
Class
Dependency
Realization
name
Class B
Association
(relationship)
Inheritance
(Generalization)
(is-a, kind-of)
Aggregation
(Part-Of)
Associations



A semantic relationship between two or more
classes that specifies connections among their
instances
A structural relationship specifying that objects
of one class are connected to objects of a
second (possibly the same) class
Example: “An Employee works for a
Company”
Associations (cont.)

An association between two classes indicates
that objects at one end of an association
“recognize” objects at the other end and may
send messages to them
Borrower
1
currBorr
3
bk[]
Book
Association
(Java implementation)
public class Borrower
{
Book bk[];
int numBooks;
…
public Borrower()
{ numBooks = 0;
bk = new Book[3];
}
// methods that update bk
public void borrowBook( Book
b )
{ bk[numBooks] = b;
numBooks++;
b.setBorrower( this );
}
}
public class Book
{
Borrower currBorr;
public void setBorrower(
Borrower bw )
{ currBorr = bw;
}
}
Aggregation

A special form of association that models a
whole-part relationship between an aggregate
(the whole) and its parts.

Models a “is a part-part of” relationship.
4
Car
Wheel
wheels
Whole
Part
Aggregation
(Java implementation)
public class Car
{
private Wheel wheels[];
...
// wheel objects are created externally and
// passed to the constructor
public Car( Wheel w1, Wheel w2, … )
{
// we can check w1, w2, etc. for null
// to make sure wheels exist
wheels = new Wheel[4];
wheels[0] = w1;
wheels[1] = w2;
…
}
}
Composition

A strong form of aggregation

The whole is the sole owner of its part



The part object may belong to only one whole
Multiplicity on the whole side must be one
The life time of the part is dependent upon the
whole

The composite must manage the creation and
destruction of its parts
Line
Polygon
2
Point
3..*
Composition
(C++ implementations)


class Circle {
public:
Circle() : center(1,2) {}
private:
Point center;
};
class Circle {
public:
Circle() : center(new Point(1,2)) {}
~Circle() { delete center; }
private:
Point* const center;
};
Composition
(Java implementations)
public class Car
{
private Wheel wheels[];
...
public Car()
{
wheels = new Wheel[4];
// Wheels are created in Car …
wheels[0] = new Wheel();
wheels[1] = new Wheel();
…
}
...
}
Generalization

Indicates that objects of the specialized class
(subclass) are substitutable for objects of the
generalized class (super-class)

“is kind of” relationship
An abstract
class
Shape
Super
Class
Circle
Sub
Class
Generalization
relationship
Generalization

A sub-class inherits from its super-class




Attributes
Operations
Relationships
A sub-class may



Add attributes and operations
Add relationships
Refine (override) inherited operations
Generalization
(Java implementation)


public abstract class Shape
{
public abstract void draw();
...
}
public class Circle extends Shape
{
public void draw() { ... }
...
}
Dependency


A dependency indicates a semantic relation
between two or more classes in which a
change in one may force changes in the other
although there is no explicit association
between them
A stereotype may be used to denote the type of
the dependency
Bank
processTransactions ()
uses
Parser
getTransaction()
Dependency
(Java implementation)
public class Bank
{
…
public void processTransactions()
{
// Parser p is a local variable …
Parser p = new Parser(…);
…
p.getTransaction();
…
}
…
}
Realization



A realization relationship indicates that one
class implements a behavior specified by
another class (an interface or protocol).
An interface can be realized by many classes.
A class may realize many interfaces.
LinkedList
<<interface>>
List
LinkedList
List
Realization
(Java implementation)


public interface List
{
boolean add(Object o);
...
}
public class LinkedList implements List
{
public boolean add(Object o)
{
...
}
...
}
Basic Class Diagram (Example)
Head
Arm
Person
Class
Student
takes
Basic Class Diagram (Example)
Person
+ name : String
- ssn : String
# birthday : Date
/ age : int
+getName : String
-calculateAge : int
Class Diagrams (Advanced)
Cardinality (Multiplicity)
1
0..1
0..n
1..n
*
Student
Class
takes
0..n
Class Diagrams (Advanced)
Important Stereotypes:
<<interface>> specify collection of operations to specify
services
<<type>> specify structure and behavior (not implementation)
<<enumeration>> specify discrete values
<<implementationClass>> helper class created in detail design
<<interface>>
ImageReader
readImage
writeImage
<<type>>
Complex
Fundamental
Attributes
Fundamental
behavior
<<enumeration>>
Status
Idle
Working
Error
Class Diagrams (Advanced)
Simple Aggregation (object lifetime)
Composite Aggregation (unique member)
Can have self-relations:
manager
employee
manages
Exception handling:
<<sends>>
Class Diagrams (Advanced)
Book
Patron
Checks Out
Association
Class
TVRS Example
TrafficPoliceman 1 issues *
TrafficReport
id : long
description : String
occuredAt : Date
reports of
1..*
Policeman
id : long
name : String
rank : int
<<abstract>>
Violation
id : long
description : String
Offender
1..*
1
name : String
id : long
Class Diagrams (Advanced)
Role
name
StaffMember
instructor
1..*
Multiplicity
Association
name
instructs
*
Navigable
(uni-directional)
association
Student
Role
*
Courses
Reflexive
association
pre requisites
0..3
Class Diagram Hints





Provide abstraction of problem domain
Embodies small set of well-defined
responsibilities
Clear separation between specification and
implementation
Understandable and simple
Show only important properties
Class Hints





Organize similar classes into packages
Beware of cyclical generalization
Use associations where there are structural
relationships
Associations are NOT comm pipes!!!!!!!
Start with Analysis, then refine details
Sequence Diagrams






AKA Interaction Diagrams – Semantically equivalent
to Collaboration Diagrams
Dynamic Model relating use cases and class diagrams
Illustrates how objects interacts with each other
Shows time ordering of interactions
Generally a set of messages between collaborating
objects
Ordering of objects not significant
Sequence Diagrams



Show only one flow of control
Can model simple sequential flow, branching,
iteration, recursion and concurrency
May need multiple diagrams



Primary
Variant
Exceptions
Sequence Diagram (Basic)
Object : Class or Actor
Lifeline
Focus of
Control/
Activation
X
Object
Destruction/
Termination
name
message
<<create>>
<<destroy>>
Sequence Diagram (Basic)
Student
aClass:
Class
Register
:Scheduler
adjustRoom
checkRooms
Sequence Diagram (Basic)
X-Axis (objects)
member:
LibraryMember
borrow(book)
Y-Axis (time)
ok = mayBorrow()
message
[ok] borrow(member)
condition
:Book
Copy
book:Book
Life
Line
setTaken(member)
Object
Activation
box
Sequence Diagrams (Advanced)
Seq# [Guard] *[Iteration] Return-List :=
Operation-Name (Argument-List)
*[Iteration Condition]
recursion
Conditional
Lifeline
{transient}
Object

Object naming:




syntax: [instanceName][:className]
Name classes consistently with your
class diagram (same classes).
Include instance names when objects
are referred to in messages or when
several objects of the same type exist in
the diagram.
The Life-Line represents the object’s
life during the interaction
myBirthdy
:Date
Messages


An interaction between two objects is
performed as a message sent from one object
to another (simple operation call, Signaling,
RPC)
If object obj1 sends a message to another
object obj2 some link must exist between those
two objects (dependency, same objects)
Messages (Cont.)

A message is represented by an arrow between
the life lines of two objects



Self calls are also allowed
The time required by the receiver object to process
the message is denoted by an activation-box
A message is labeled at minimum with the
message name

Arguments and control information (conditions,
iteration) may be included
Return Values

Optionally indicated using a dashed arrow with
a label indicating the return value.



Don’t model a return value when it is obvious
what is being returned, e.g. getTotal()
Model a return value only when you need to refer
to it elsewhere, e.g. as a parameter passed in
another message.
Prefer modeling return values as part of a method
invocation, e.g. ok = isValid()
Sequence Diagram (Basic)
:Violations
Dialog
:Violations
Controller
:Violations
DBProxy
Clerk
lookup
viewButton()
Lookup
Traffic
Violation
id=getID()
getViolation(id)
May be a
pseudomethod
display(v)
v
<<create>>
DB is queried
and the result
is returned as
an object
v:Traffic
Violation
Sequence Diagram (Basic)
Active
object
:PrintServer
:Queue
Client
print(doc,client)
Repeated
forever with 1
min interludes
[job] done(status)
enqueue(job)
job=dequeue()
[job]print(job.doc)
status
:Printer
Proxy