Class Diagrams
CS 123/CS 231
Classes in a Class Diagram
Class name only Example
With Details Example
Class Name
Class Nameattributesmethods
BankAccount
Bank Accountdouble balance
deposit()withdraw()
Relationships
Inheritance (arrow)example: between Secretary and
EmployeeComposition/Aggregation (diamond)
example: between Car and WheelAssociation (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 Details
CardinalitySpecifies the number of objects that
may participate in the relationshipRoles and Navigability
Specifies relationship name and accessAggregation versus CompositionDependencies
Cardinality
Also known as multiplicityExact 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 Navigability
Role name placed on the side of a participant
Let 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 objects
An 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 }
Dependencies
Some 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 } …}
Top Related