Class Diagrams

15
Class Diagrams CS 123/CS 231

description

Class Diagrams. CS 123/CS 231. Classes in a Class Diagram. Class name onlyExample With DetailsExample. Bank Account. Class Name. Bank Account double balance deposit() withdraw(). Class Name attributes methods. Relationships. Inheritance (arrow) - PowerPoint PPT Presentation

Transcript of Class Diagrams

Page 1: Class Diagrams

Class Diagrams

CS 123/CS 231

Page 2: Class Diagrams

Classes in a Class Diagram

Class name only Example

With Details Example

Class Name

Class Nameattributesmethods

BankAccount

Bank Accountdouble balance

deposit()withdraw()

Page 3: Class Diagrams

Relationships

Inheritance (arrow)example: between Secretary and

EmployeeComposition/Aggregation (diamond)

example: between Car and WheelAssociation (line)

example: between Borrower and Book

Page 4: Class Diagrams

Inheritance

Secretary

Employee

public class Secretary extends Employee { …}

Page 5: Class Diagrams

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

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

Notational Details

CardinalitySpecifies the number of objects that

may participate in the relationshipRoles and Navigability

Specifies relationship name and accessAggregation versus CompositionDependencies

Page 8: Class Diagrams

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

Page 9: Class Diagrams

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

Page 10: Class Diagrams

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

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

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

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

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

Page 15: Class Diagrams

Dependency Example

Parser

getOrder()

usesRestaurant

processOrders()

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