170 likes | 315 Views
Class Diagrams. CS 123/CS 231. Classes in a Class Diagram. Class name only Example With Details Example. Bank Account. Class Name. Bank Account double balance deposit() withdraw(). Class Name attributes methods. Relationships. Inheritance (arrow)
E N D
Class Diagrams CS 123/CS 231
Classes in a Class Diagram • Class name only Example • With Details Example Bank Account Class Name Bank Account double balance deposit() withdraw() Class Name attributes methods
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 Wheel 4 w[] public class Car { Wheel w[]; ... public Car() { w = new Wheel[4]; … } ... } Note: [ ] in diagram is sometimes left out since w does not need to be an array
Association Borrower Book 1 3 currBorr bk[] public class Borrower { Book bk[]; … public Borrower() { bk = new Book[3]; } } 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 PriceChecker getPrice() pc public class FastFoodCounter { PriceChecker pc; … public void add( … ) { … double pr = pc.getPrice(); … } … } public class PriceChecker { // no access to counter }
Bi-directional Navigability Borrower Book 1 3 currBorr bk[] public class Borrower { Book bk[]; … public Borrower() { bk = new Book[3]; } } public class Book { Borrower currBorr; … } Note: double arrowheads may be omitted (bi-directionalnavigability 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 Wheel 4 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 Restaurant processOrders() Parser getOrder() uses public class Restaurant { … public void processOrders() { Parser p = new Parser(…); // call getOrder() in this method } … }