UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

45
UML Class Diagrams UML Class Diagrams Sequence Diagrams Sequence Diagrams CSE230 CSE230 Dae-Kyoo Kim Dae-Kyoo Kim

description

Class Diagrams A class diagram shows: A class diagram shows: Classes Classes Attributes Attributes Methods Methods Interfaces Interfaces Collaborations Collaborations Relationships: Dependency, Generalization, Association Relationships: Dependency, Generalization, Association A class diagram is a STATIC view of system A class diagram is a STATIC view of system

Transcript of UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

Page 1: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

UML Class DiagramsUML Class DiagramsSequence DiagramsSequence Diagrams

CSE230CSE230Dae-Kyoo KimDae-Kyoo Kim

Page 2: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

AgendaAgenda Class DiagramsClass Diagrams

SymbologySymbology Basic ModelingBasic Modeling Intermediate ModelingIntermediate Modeling

Sequence DiagramsSequence Diagrams SymbologySymbology

Page 3: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 4: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

Basic Class DiagramsBasic Class Diagrams

Windowsize: Sizevisibility: booleandisplay()hide()

Class Name

Attributes

Operations

Page 5: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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>>…

Page 6: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 7: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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; } ...}

Page 8: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 9: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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”

Page 10: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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[]

Page 11: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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; }}

Page 12: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 13: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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; … }}

Page 14: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 15: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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;};

Page 16: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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(); … } ...}

Page 17: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 18: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 19: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

GeneralizationGeneralization(Java implementation)(Java implementation)

public abstract class Shape{ public abstract void draw(); ...}

public class Circle extends Shape { public void draw() { ... } ...}

Page 20: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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 ()

Page 21: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

DependencyDependency(Java implementation)(Java implementation)

public class Bank{ … public void processTransactions() { // Parser p is a local variable … Parser p = new Parser(…); … p.getTransaction(); … } …}

Page 22: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 23: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

RealizationRealization(Java implementation)(Java implementation)

public interface List{ boolean add(Object o); ...}

public class LinkedList implements List { public boolean add(Object o)

{ ...

}...

}

Page 24: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

Basic Class Diagram (Example)Basic Class Diagram (Example)Person

Student Class

takes

Head

Arm

Page 25: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

Basic Class Diagram (Example)Basic Class Diagram (Example)

Person

+ name : String- ssn : String# birthday : Date/ age : int

+getName : String-calculateAge : int

Page 26: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

Class Diagrams (Advanced)Class Diagrams (Advanced)

Cardinality (Multiplicity) 10..10..n1..n*

Student Class

takes 0..n

Page 27: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 28: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

Class Diagrams (Advanced)Class Diagrams (Advanced)

Simple Aggregation (object lifetime)

Composite Aggregation (unique member)

Can have self-relations:

employeemanagesmanager

Exception handling:

<<sends>>

Page 29: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

Class Diagrams (Advanced)Class Diagrams (Advanced)

Patron Book

Checks Out

AssociationClass

Page 30: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 31: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

*

Page 32: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 33: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 34: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 35: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 36: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

Sequence Diagram (Basic)Sequence Diagram (Basic)

Object : Class or Actor

Lifeline

message

name

X ObjectDestruction/Termination

<<create>><<destroy>>

Focus of Control/Activation

Page 37: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

Sequence Diagram (Basic)Sequence Diagram (Basic)

Student

aClass:Class

Register

:Scheduler

adjustRoom checkRooms

Page 38: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 39: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

Sequence Diagrams (Advanced)Sequence Diagrams (Advanced)

Seq# [Guard] *[Iteration] Return-List := Operation-Name (Argument-List)

recursion

*[Iteration Condition]

ConditionalLifeline {transient}

Page 40: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 41: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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)

Page 42: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 43: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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()

Page 44: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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

Page 45: UML Class Diagrams Sequence Diagrams CSE230 Dae-Kyoo Kim.

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