Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample...

Preview:

DESCRIPTION

Relationships zInheritance (arrow) Õexample: between Secretary and Employee zComposition/Aggregation (diamond) Õexample: between Car and Wheel zAssociation (line) Õexample: between Borrower and Book

Citation preview

Class Diagrams

Software Design and Development

Classes in a Class DiagramClass name only Example

With Details Example

Class Name

Class Nameattributesmethods

BankAccount

Bank Accountdouble balance

deposit()withdraw()

RelationshipsInheritance (arrow)

example: between Secretary and Employee

Composition/Aggregation (diamond)example: between Car and Wheel

Association (line)example: between Borrower and Book

Inheritance

Secretary

Employee

public class Secretary extends Employee { …}

Composition/Aggregation

Car Wheel4

w[]

public class Car { Wheel w[]; ... public Car() { w = new Wheel[4]; … } ...}

Note: [ ] in diagramis sometimes left outsince w does not needto be an array

Association

Borrower BookcurrBorr bk[]

31

public class Borrower { Book bk[]; … public Borrower() { bk = new Book[3]; }}

public class Book { Borrower currBorr; …}

Notational DetailsCardinality

Specifies the number of objects that may participate in the relationship

Roles and NavigabilitySpecifies relationship name and access

Aggregation versus CompositionDependencies

CardinalityAlso known as multiplicity

Exact number (mandatory)Range (e.g., 0..5)* (many-valued)

Specifies the number of objects that may be associated with an object of the other class

For associations, multiplicity is specified on both participants

Roles and NavigabilityRole name placed on the side of a participantLet A and B be associated classes and let rrr

be the role specified on B’s side rrr is the role of B in the relationship rrr is a member in class A rrr refers to one or more (depending on

multiplicity) B objectsAn arrowhead indicates the ability to access

B participant(s) from A

Uni-directional Navigability

PriceChecker

getPrice()

pcFastFoodCounter

public class FastFoodCounter { PriceChecker pc; … public void add( … ) { … double pr = pc.getPrice(); … } …}

public class PriceChecker { // no access to counter}

Bi-directional Navigability

Borrower BookcurrBorr bk[]

31

public class Borrower { Book bk[]; … public Borrower() { bk = new Book[3]; }}

public class Book { Borrower currBorr; …}

Note: double arrowheads maybe omitted (bi-directionalnavigability assumed)

Aggregation versus Composition Part-of relationshipsAggregation

Part may be independent of the whole but the whole requires the part

Unfilled diamondComposition (“stronger” form of aggregation)

Part is created and destroyed with the wholeFilled diamond

Definitions and distinctions between aggregation and composition still “under debate”

Mandatory Parts

Car Wheel4

wheels

public class Car {private Wheel wheels[4]; // wheel objects are created externally ...public Car(Wheel w1, Wheel w2, … ) … // wheels required in constructor // w1, w2, … will be checked for null values }

DependenciesSome classes use other classes but

are not related to them in ways previously discussed

Not relationships in the sense that participants do not become attributes in another class

Most common example:As local variables in (or arguments to) a

method of the class

Dependency Example

Parser

getOrder()

usesRestaurant

processOrders()

public class Restaurant { … public void processOrders() { Parser p = new Parser(…); // call getOrder() in this method } …}

Use Cases andObject Interaction

Software Design and Development

Depicting System BehaviorFirst, identify the use cases

Use case: typical interaction between a user and the system

Use Case DiagramDepicts all use cases for a system

Interaction DiagramDepicts a single use case as a collection

of interacting objects

Example:Use Case Diagram

Facilitate Checkout

Facilitate Return

Search for Book

LIBRARY SYSTEM

BorrowerLibrarian

Use Case Diagram NotationStick Figures – Actors

Could be a human user or a subsystemEllipses - Use CasesLinks - between actors and use casesLinks between use cases

<<uses>>: to depict inclusion of a use case

<<extends>>: to depict variations of a general use case

Example: <<uses>>

Facilitate Checkout

Facilitate Return

Librarian Log-in<<uses>>

<<uses>>

Note: UML v.1.3uses <<includes>>instead of <<uses>>

Example: <<extends>>

Search for Bookquery

Borrower

By Author

By Subject

<<extends>>

<<extends>>

Note: query iscalled anextension point

Describing a Use CaseNarrative

Library example (Facilitate Checkout): Given a borrower’s ID Card and the book to be borrowed, the librarian enters the borrower’s ID number and the book’s catalogue number. If the borrower is allowed to check out the book, the system displays that the book has been recorded as borrowed

Or, an Interaction Diagram

Example:Interaction Diagram

CheckoutScreen

:Borrower

:Book

1: checkIfDelinquent()3: borrowBook()

2: checkIfAvailable()

4: setBorrower()

Interaction (Collaboration) Diagram NotationRectangles: Classes/ObjectsArrows: Messages/Method CallsLabels on Arrows

sequence number (whole numbers or X.X.X notation)

method name (the message passed)more details, if helpful and necessary

(iterators, conditions, parameters, types, return types)

MethodsInteraction Diagrams suggest/imply

methods for classesHas consequences on detailed class diagram

The label(s) of an arrow should be a method of the class the arrow points to

Library SystemBorrower class should have at least two

methods (checkIfDelinquent and borrowBook)

Including Conditionsand Types

CheckoutScreen

r:Borrower

b:Book

1: delinq = checkIfDelinquent():boolean3:[!delinq & avail] borrowBook(Book b)

2: avail = checkIfAvailable():boolean

4: setBorrower( Borrower bk )

Interaction Diagramsand Object-Oriented CodeNote correspondences between messages

passed and method calls in actual codeExample:

borrowBook() is defined in Borrower and is called from the code in CheckOutScreen

setBorrower() is defined in Book and is called from borrowBook() method of Borrower

Other diagramming details imply if statements and loops in code

Creating an Objectnew means a constructor is being

calledImplies object creation

:Customer

:CustomerList

1: addCustomer(custdetails)

:Encoder

2: newNote: this means theaddCustomer method willcontain code that createsa Customer object

Iteration* is an iterator

means the method is called repeatedly

:Branch

:Store1: printSalesSummary()

:Manager

2: * getTotalSales()

Note: Store needsdata from all branchesto produce a summary

SummaryProvide a Use Case Diagram to depict the use

cases of a systemFor each use case, describe and provide an

Interaction DiagramDepict use case as a collection of interacting

objectsOther diagramming techniques that aid in

building a dynamic model:State diagram: describes object state changesActivity diagram: describes method behavior

Recommended