UML Class DiagramsUML Class DiagramsSequence DiagramsSequence Diagrams
CSE230CSE230Dae-Kyoo KimDae-Kyoo Kim
AgendaAgenda Class DiagramsClass Diagrams
SymbologySymbology Basic ModelingBasic Modeling Intermediate ModelingIntermediate Modeling
Sequence DiagramsSequence Diagrams SymbologySymbology
Class DiagramsClass Diagrams A class diagram shows:A class diagram shows:
ClassesClasses AttributesAttributes MethodsMethods
InterfacesInterfaces CollaborationsCollaborations Relationships: Dependency, Generalization, Relationships: Dependency, Generalization,
AssociationAssociation A class diagram is a STATIC view of systemA class diagram is a STATIC view of system
Basic Class DiagramsBasic Class Diagrams
Windowsize: Sizevisibility: booleandisplay()hide()
Class Name
Attributes
Operations
Basic Class DiagramsBasic Class Diagrams
+ public# protected- private
/ derived$ static
Visibility Attribute Name [Multiplicity]:Type = Initial Value
Visibility Method Name (Parameter List) : Return-List
Class Scope Variable
Abstract<<abstract>>
<<constructor>><<query>><<update>>…
Attribute Example Attribute Example (Java implementation)(Java implementation)
public class Note{ public String author = “unknown”; public String text; private static long total = 0; ...}
+ author: String = “unknown”+ text : String- total : long = 0
Note
Operation ExampleOperation Example (Java implementation) (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 DiagramsBasic Class Diagrams
Superclass
Subclass
Inheritance(Generalization)(is-a, kind-of)
Class with parts
AssemblyClass
Aggregation(Part-Of)
Association(relationship)
Class with parts
AssemblyClass
Dependency
Class A
Class B
name
Interface
ConcreteClass
Realization
AssociationsAssociations A semantic relationship between two or more A semantic relationship between two or more
classes that specifies connections among their classes that specifies connections among their instancesinstances
A structural relationship specifying that A structural relationship specifying that objects of one class are connected to objects of objects of one class are connected to objects of a second (possibly the same) classa second (possibly the same) class
Example: “An Employee works for a Example: “An Employee works for a Company”Company”
Associations (cont.)Associations (cont.) An association between two classes indicates An association between two classes indicates
that objects at one end of an association that objects at one end of an association “recognize” objects at the other end and may “recognize” objects at the other end and may send messages to themsend messages to them
Borrower Book31currBorr bk[]
AssociationAssociation(Java implementation)(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; }}
AggregationAggregation A special form of association that models a A special form of association that models a
whole-part relationship between an aggregate whole-part relationship between an aggregate (the whole) and its parts.(the whole) and its parts. Models a “is a part-part of” relationship.Models a “is a part-part of” relationship.
Whole Part
Car Wheel4
wheels
AggregationAggregation(Java implementation)(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; … }}
CompositionComposition A strong form of aggregationA strong form of aggregation
The whole is the sole owner of its partThe whole is the sole owner of its part The part object may belong to only one wholeThe part object may belong to only one whole
Multiplicity on the whole side must be oneMultiplicity on the whole side must be one The life time of the part is dependent upon the The life time of the part is dependent upon the
wholewhole The composite must manage the creation and The composite must manage the creation and
destruction of its partsdestruction of its partsLine Point
3..*
2
Polygon
CompositionComposition(C++ implementations)(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;};
CompositionComposition(Java implementations)(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(); … } ...}
GeneralizationGeneralization Indicates that objects of the specialized class Indicates that objects of the specialized class
(subclass) are substitutable for objects of the (subclass) are substitutable for objects of the generalized class (super-class)generalized class (super-class) ““is kind of” relationshipis kind of” relationship
Shape
Circle
Super Class
Sub Class
An abstract class
Generalization relationship
GeneralizationGeneralization A sub-class inherits from its super-classA sub-class inherits from its super-class
AttributesAttributes OperationsOperations RelationshipsRelationships
A sub-class mayA sub-class may Add attributes and operationsAdd attributes and operations Add relationshipsAdd relationships Refine (override) inherited operationsRefine (override) inherited operations
GeneralizationGeneralization(Java implementation)(Java implementation)
public abstract class Shape{ public abstract void draw(); ...}
public class Circle extends Shape { public void draw() { ... } ...}
DependencyDependency A dependency indicates a semantic relation A dependency indicates a semantic relation
between two or more classes in which a between two or more classes in which a change in one may force changes in the other change in one may force changes in the other although there is no explicit association although there is no explicit association between thembetween them
A stereotype may be used to denote the type of A stereotype may be used to denote the type of the dependencythe dependency
Parser
getTransaction()usesBank
processTransactions ()
DependencyDependency(Java implementation)(Java implementation)
public class Bank{ … public void processTransactions() { // Parser p is a local variable … Parser p = new Parser(…); … p.getTransaction(); … } …}
RealizationRealization A realization relationship indicates that one A realization relationship indicates that one
class implements a behavior specified by class implements a behavior specified by another class (an interface or protocol).another class (an interface or protocol).
An interface can be realized by many classes.An interface can be realized by many classes. A class may realize many interfaces.A class may realize many interfaces.
LinkedList<<interface>>
List LinkedList List
RealizationRealization(Java implementation)(Java implementation)
public interface List{ boolean add(Object o); ...}
public class LinkedList implements List { public boolean add(Object o)
{ ...
}...
}
Basic Class Diagram (Example)Basic Class Diagram (Example)Person
Student Class
takes
Head
Arm
Basic Class Diagram (Example)Basic Class Diagram (Example)
Person
+ name : String- ssn : String# birthday : Date/ age : int
+getName : String-calculateAge : int
Class Diagrams (Advanced)Class Diagrams (Advanced)
Cardinality (Multiplicity) 10..10..n1..n*
Student Class
takes 0..n
Class Diagrams (Advanced)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
<<type>>Complex
readImagewriteImage
Fundamentalbehavior
FundamentalAttributes
<<enumeration>>Status
IdleWorkingError
Class Diagrams (Advanced)Class Diagrams (Advanced)
Simple Aggregation (object lifetime)
Composite Aggregation (unique member)
Can have self-relations:
employeemanagesmanager
Exception handling:
<<sends>>
Class Diagrams (Advanced)Class Diagrams (Advanced)
Patron Book
Checks Out
AssociationClass
TVRS Example
id : longname : Stringrank : int
Policeman
<<abstract>>
TrafficPoliceman id : longdescription : String
TrafficReport
id : longdescription : String
Violation
name : Stringid : long
Offender1..* 1
reports of
1..*
issues1 *
occuredAt : Date
Class Diagrams (Advanced)Class Diagrams (Advanced)
StaffMember Student1..* *instructs
instructor
Association name
Role name
MultiplicityNavigable
(uni-directional) association
Courses
pre - requisites
0..3Reflexive
association
Role
*
Class Diagram HintsClass Diagram Hints Provide abstraction of problem domainProvide abstraction of problem domain Embodies small set of well-defined Embodies small set of well-defined
responsibilitiesresponsibilities Clear separation between specification and Clear separation between specification and
implementationimplementation Understandable and simpleUnderstandable and simple Show only important propertiesShow only important properties
Class HintsClass Hints Organize similar classes into packagesOrganize similar classes into packages Beware of cyclical generalizationBeware of cyclical generalization Use associations where there are structural Use associations where there are structural
relationshipsrelationships Associations are NOT comm pipes!!!!!!!Associations are NOT comm pipes!!!!!!! Start with Analysis, then refine detailsStart with Analysis, then refine details
Sequence DiagramsSequence Diagrams AKA Interaction Diagrams – Semantically equivalent AKA Interaction Diagrams – Semantically equivalent
to Collaboration Diagramsto Collaboration Diagrams Dynamic Model relating use cases and class diagramsDynamic Model relating use cases and class diagrams Illustrates how objects interacts with each otherIllustrates how objects interacts with each other Shows time ordering of interactionsShows time ordering of interactions Generally a set of messages between collaborating Generally a set of messages between collaborating
objectsobjects Ordering of objects not significantOrdering of objects not significant
Sequence DiagramsSequence Diagrams Show only one flow of controlShow only one flow of control Can model simple sequential flow, branching, Can model simple sequential flow, branching,
iteration, recursion and concurrencyiteration, recursion and concurrency May need multiple diagramsMay need multiple diagrams
PrimaryPrimary VariantVariant ExceptionsExceptions
Sequence Diagram (Basic)Sequence Diagram (Basic)
Object : Class or Actor
Lifeline
message
name
X ObjectDestruction/Termination
<<create>><<destroy>>
Focus of Control/Activation
Sequence Diagram (Basic)Sequence Diagram (Basic)
Student
aClass:Class
Register
:Scheduler
adjustRoom checkRooms
Sequence Diagram (Basic)Sequence Diagram (Basic)
member:LibraryMember book:Book :Book
Copy
borrow(book)ok = mayBorrow()
[ok] borrow(member)setTaken(member)
X-Axis (objects)
Y-A
xis (tim
e)
ObjectLife Linemessage
Activation box
condition
Sequence Diagrams (Advanced)Sequence Diagrams (Advanced)
Seq# [Guard] *[Iteration] Return-List := Operation-Name (Argument-List)
recursion
*[Iteration Condition]
ConditionalLifeline {transient}
ObjectObject Object naming:Object naming:
syntax: syntax: [instanceName][:className][instanceName][:className] Name classes consistently with your Name classes consistently with your
class diagram (same classes).class diagram (same classes). Include instance names when objects Include instance names when objects
are referred to in messages or when are referred to in messages or when several objects of the same type exist in several objects of the same type exist in the diagram.the diagram.
The The Life-LineLife-Line represents the object’s represents the object’s life during the interactionlife during the interaction
myBirthdy:Date
MessagesMessages An interaction between two objects is An interaction between two objects is
performed as a message sent from one object performed as a message sent from one object to another (simple operation call, Signaling, to another (simple operation call, Signaling, RPC) RPC)
If object objIf object obj11 sends a message to another sends a message to another object objobject obj22 some link must exist between those some link must exist between those two objects (dependency, same objects)two objects (dependency, same objects)
Messages (Cont.)Messages (Cont.) A message is represented by an arrow between A message is represented by an arrow between
the life lines of two objectsthe life lines of two objects Self calls are also allowedSelf calls are also allowed The time required by the receiver object to process The time required by the receiver object to process
the message is denoted by an the message is denoted by an activation-boxactivation-box A message is labeled at minimum with the A message is labeled at minimum with the
message namemessage name Arguments and control information (conditions, Arguments and control information (conditions,
iteration) may be includediteration) may be included
Return ValuesReturn Values Optionally indicated using a dashed arrow Optionally indicated using a dashed arrow
with a label indicating the return value.with a label indicating the return value. Don’t model a return value when it is obvious Don’t model a return value when it is obvious
what is being returned, e.g. getTotal()what is being returned, e.g. getTotal() Model a return value only when you need to refer Model a return value only when you need to refer
to it elsewhere, e.g. as a parameter passed in to it elsewhere, e.g. as a parameter passed in another message.another message.
Prefer modeling return values as part of a method Prefer modeling return values as part of a method invocation, e.g. invocation, e.g. ok = isValid()ok = isValid()
Sequence Diagram (Basic)Sequence Diagram (Basic)
getViolation(id)
Clerk
:ViolationsDialog
:ViolationsController
:ViolationsDBProxy
lookupviewButton()
id=getID()
v:TrafficViolation
display(v)
<<create>>
v
Lookup Traffic Violation
May be a pseudo-method
DB is queried and the result is returned as an object
Sequence Diagram (Basic)Sequence Diagram (Basic)
print(doc,client)Client
:PrintServer :Queue :PrinterProxy
enqueue(job)
status
job=dequeue()
[job]print(job.doc)
[job] done(status)
Repeated forever with 1 min interludes
Active object
Top Related