Class Diagrams

Download Report

Transcript Class Diagrams

Class Diagrams

CS 124

Classes in a Class Diagram

 Class name only Class Name  With Details Example Bank Account Example Class Name attributes methods Bank Account double balance deposit() withdraw()

Relationships

 Inheritance (arrow)  example: between Secretary and Employee  Composition/Aggregation (diamond)  example: between Car and Wheel  Association (line)  example: between Borrower and Book

Inheritance

Employee

public class Secretary extends Employee { … }

Secretary

Composition/Aggregation

Car

public class Car { Wheel w[]; ...

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

}

4 w[] Wheel Note: [ ] in diagram is sometimes left out since

w

does not need to be an array

Association

Borrower 0..1

currBorr

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

0..3

bk[] Book

public class Book { Borrower currBorr; … }

Notational Details

 Cardinality  Specifies the number of objects that may participate in the relationship  Roles and Navigability  Specifies relationship name and access  Aggregation versus Composition  Dependencies

Cardinality

 Also 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 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

FastFood Counter

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

pc PriceChecker getPrice()

public class PriceChecker { } // no access to counter

Bi-directional Navigability

Borrower 0..1

currBorr 0..3

bk[] Book

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

Note: double arrowheads may be omitted (bi-directional navigability assumed)

Aggregation versus Composition

   

Part-of

relationships Aggregation  Part may be independent of the whole but the whole requires the part  Unfilled diamond Composition (“stronger” form of aggregation)   Part is created and destroyed with the whole Filled diamond Definitions and distinctions between aggregation and composition still “under debate”

Mandatory Parts

Car 4 wheels Wheel

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

Restaurant processOrders() uses Parser getOrder()

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