1 / 15

Class Diagrams

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)

Download Presentation

Class Diagrams

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class Diagrams CS 123/CS 231

  2. 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

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

  4. Inheritance Employee public class Secretary extends Employee { … } Secretary

  5. 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

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

  7. 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

  8. 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

  9. 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

  10. 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 }

  11. 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)

  12. 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”

  13. 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 }

  14. 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

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

More Related