30
Class Diagrams Software Design and Development

Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Embed Size (px)

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

Page 1: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Class Diagrams

Software Design and Development

Page 2: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Classes in a Class DiagramClass name only Example

With Details Example

Class Name

Class Nameattributesmethods

BankAccount

Bank Accountdouble balance

deposit()withdraw()

Page 3: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

RelationshipsInheritance (arrow)

example: between Secretary and Employee

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

Association (line)example: between Borrower and Book

Page 4: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Inheritance

Secretary

Employee

public class Secretary extends Employee { …}

Page 5: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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

Page 6: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Association

Borrower BookcurrBorr bk[]

31

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

public class Book { Borrower currBorr; …}

Page 7: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Notational DetailsCardinality

Specifies the number of objects that may participate in the relationship

Roles and NavigabilitySpecifies relationship name and access

Aggregation versus CompositionDependencies

Page 8: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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

Page 9: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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

Page 10: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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}

Page 11: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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)

Page 12: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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”

Page 13: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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 }

Page 14: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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

Page 15: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Dependency Example

Parser

getOrder()

usesRestaurant

processOrders()

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

Page 16: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Use Cases andObject Interaction

Software Design and Development

Page 17: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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

Page 18: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Example:Use Case Diagram

Facilitate Checkout

Facilitate Return

Search for Book

LIBRARY SYSTEM

BorrowerLibrarian

Page 19: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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

Page 20: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Example: <<uses>>

Facilitate Checkout

Facilitate Return

Librarian Log-in<<uses>>

<<uses>>

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

Page 21: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Example: <<extends>>

Search for Bookquery

Borrower

By Author

By Subject

<<extends>>

<<extends>>

Note: query iscalled anextension point

Page 22: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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

Page 23: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

Example:Interaction Diagram

CheckoutScreen

:Borrower

:Book

1: checkIfDelinquent()3: borrowBook()

2: checkIfAvailable()

4: setBorrower()

Page 24: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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)

Page 25: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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)

Page 26: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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 )

Page 27: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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

Page 28: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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

Page 29: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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

Page 30: Class Diagrams Software Design and Development. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods

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