230 likes | 327 Views
UML Class Diagram. A class diagram shows Classes The relationships between them. Classes. Class name Class attributes (state) Class methods (operations) Visibility + means public # means protected − means private ~ means package. Course. name: String number: Integer.
E N D
A class diagram shows • Classes • The relationships between them
Classes • Class name • Class attributes (state) • Class methods (operations) • Visibility • + means public • # means protected • − means private • ~ means package Course name: String number: Integer addStudent()
Class Details • Abstract class: name in italics • Member properties: • static • abstract • CONSTANT • Specify type of attributes • Specify signature and return value of methods • addStudent( s : Student ) : void
Inheritance Shape -color: Color +setColor(c: Color): void +getArea(): double Think “tree” not “arrow” Circle -radius: double override +setRadius(r: double): void +getRadius(): double +getArea(): double
Interfaces «interface» Drawable +draw(): void Note dashed line Circle -radius: double implement +setRadius(r: double): void +getRadius(): double +draw(): void
Associations • When one class knows about another class and may call methods of that class’s objects • General association • Aggregation • Composition • dependency
Association • Specify that classes know each other • Can be named (typically verbs) Professor Course teaches
Multiplicity • Specify the numerical relationship • 1 – exactly one • 0..1 – zero or one • 0..* (or just *) – any number or none • 1..*– one or more • 3..8 – three to eight Professor Course teaches 1 0..*
Association • Classes can have role names (nouns) • Typically represent attribute name in other class Professor Course teacher Class Professor { myCourse: Course; … } Class Course { teacher: Professor; … }
Association • Associations can be reflexive • In this case role names are required Professor mentor 1 mentee 0..2
Navigability • The course knows the textbook(s) used • The textbook does not know course(s) which use it • Alternative interpretation: navigability from textbook to course is unspecified • To prohibit access use Course TextBook
Association Class • A class that represents the association Student Course 0..* 0..* Enrolment grade: integer
Aggregation • One class contains an aggregate of objects from the other class • But aggregated objects are still independent • Implies container attribute Department Professor
Composition • A stronger form of aggregation • One class contains objects from the other class • These objects have no existence without the containing object Program Course 1
Dependency • One class uses another • Local in method • Argument to method • Creation of objects (e.g. factory) • But it is not an attribute (field) • Usually many; show only important ones B A C «creates» method(arg : B)
Derived Attributes • An attribute whose value is derived from other attributes • Does not have a setter Course name: String /enrolment: Integer addStudent() Student
Constraints and Notes • Provide additional information Course capacity: integer /enrolment: integer {enrolment capacity} addStudent() Student Only nice ones
Object Diagrams • Similar to class diagrams • Show object instances rather than classes • Reflect relationships at runtime • No reflexive associations • ListNode class: next is reflexive • ListNode object: next is reference to another object UML Class Diagrams