Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Diagrams CS 123/CS 231. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods Bank Account Bank.

Similar presentations


Presentation on theme: "Class Diagrams CS 123/CS 231. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods Bank Account Bank."— Presentation transcript:

1 Class Diagrams CS 123/CS 231

2 Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods Bank Account Bank Account double balance deposit() withdraw()

3 Relationships zInheritance (arrow) Õexample: between Secretary and Employee zComposition/Aggregation (diamond) Õexample: between Car and Wheel zAssociation (line) Õexample: between Borrower and Book

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

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 BorrowerBook currBorrbk[] 31 public class Borrower { Book bk[]; … public Borrower() { bk = new Book[3]; } public class Book { Borrower currBorr; … }

7 Notational Details zCardinality ÕSpecifies the number of objects that may participate in the relationship zRoles and Navigability ÕSpecifies relationship name and access zAggregation versus Composition zDependencies

8 Cardinality zAlso known as multiplicity ÕExact number (mandatory) ÕRange (e.g., 0..5) Õ* (many-valued) zSpecifies the number of objects that may be associated with an object of the other class zFor associations, multiplicity is specified on both participants

9 Roles and Navigability zRole name placed on the side of a participant zLet 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 zAn arrowhead indicates the ability to access B participant(s) from A

10 Uni-directional Navigability PriceChecker getPrice() pc FastFood Counter public class FastFoodCounter { PriceChecker pc; … public void add( … ) { … double pr = pc.getPrice(); … } … } public class PriceChecker { // no access to counter }

11 Bi-directional Navigability BorrowerBook currBorrbk[] 31 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)

12 Aggregation versus Composition zPart-of relationships zAggregation ÕPart may be independent of the whole but the whole requires the part ÕUnfilled diamond zComposition (“stronger” form of aggregation) ÕPart is created and destroyed with the whole ÕFilled diamond zDefinitions 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 zSome classes use other classes but are not related to them in ways previously discussed xNot relationships in the sense that participants do not become attributes in another class zMost common example: ÕAs local variables in (or arguments to) a method of the class

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


Download ppt "Class Diagrams CS 123/CS 231. Classes in a Class Diagram zClass name onlyExample zWith DetailsExample Class Name attributes methods Bank Account Bank."

Similar presentations


Ads by Google