Class Diagrams CS 124 Classes in a Class

  • Slides: 15
Download presentation
Class Diagrams CS 124

Class Diagrams CS 124

Classes in a Class Diagram l Class name only Class. Name l With Details

Classes in a Class Diagram l Class name only Class. Name l With Details Class. Name attributes methods Example Bank. Account double balance deposit() withdraw()

Relationships l Inheritance (arrow) l l Composition/Aggregation (diamond) l l example: between Secretary and

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

Inheritance Employee public class Secretary extends Employee { … } Secretary

Inheritance Employee public class Secretary extends Employee { … } Secretary

Composition/Aggregation Car 4 w[] public class Car { Wheel w[]; . . . public

Composition/Aggregation Car 4 w[] public class Car { Wheel w[]; . . . public Car() { w = new Wheel[4]; … }. . . } Wheel Note: [ ] in diagram is sometimes left out since w does not need to be an array

Association 0. . 1 Borrower curr. Borr public class Borrower { Book bk[]; …

Association 0. . 1 Borrower curr. Borr public class Borrower { Book bk[]; … public Borrower() { bk = new Book[3]; } } 0. . 3 bk[] Book public class Book { Borrower curr. Borr; … }

Notational Details l Cardinality l l Roles and Navigability l l l Specifies the

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

Cardinality l Also known as multiplicity l l l Exact number (mandatory) Range (e.

Cardinality l Also known as multiplicity l l l 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 l l Role name placed on the side of a participant

Roles and Navigability l l 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 l l 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 Fast. Food Counter pc Price. Checker get. Price() public class Fast. Food.

Uni-directional Navigability Fast. Food Counter pc Price. Checker get. Price() public class Fast. Food. Counter { Price. Checker pc; … public void add. Order( … ) { … double pr = pc. get. Price(); … } public class Price. Checker { // no access to counter }

Bi-directional Navigability 0. . 1 Borrower curr. Borr public class Borrower { Book bk[];

Bi-directional Navigability 0. . 1 Borrower curr. Borr public class Borrower { Book bk[]; … public Borrower() { bk = new Book[3]; } } 0. . 3 bk[] Book public class Book { Borrower curr. Borr; … } Note: double arrowheads may be omitted (bi-directional navigability assumed)

Aggregation versus Composition l l Part-of relationships Aggregation l l l Composition (“stronger” form

Aggregation versus Composition l l Part-of relationships Aggregation l l l Composition (“stronger” form of aggregation) l l l Part may be independent of the whole but the whole requires the part Unfilled diamond 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[]; //

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

Dependencies l Some classes use other classes but are not related to them in

Dependencies l Some classes use other classes but are not related to them in ways previously discussed l l Not relationships in the sense that participants do not become attributes in another class Most common example: l As local variables in (or arguments to) a method of the class

Dependency Example Restaurant process. Orders() uses Parser get. Order() public class Restaurant { …

Dependency Example Restaurant process. Orders() uses Parser get. Order() public class Restaurant { … public void process. Orders() { Parser p = new Parser(…); // call get. Order() in this method } … }