UML Class Diagram
A class diagram shows 1.Classes 2.The relationships between them
Classes Class name Class attributes (state) Class methods (operations) Visibility o + means public o # means protected o − means private o ~ means package Course name: String number: Integer addStudent()
Class Details Abstract class: name in italics Member properties: o static o abstract o 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 Circle -radius: double +setRadius(r: double): void +getRadius(): double +getArea(): double Think “tree” not “arrow” override
Interfaces «interface» Drawable +draw(): void Circle -radius: double +setRadius(r: double): void +getRadius(): double +draw(): void implement Note dashed line
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) CourseProfessor 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 CourseProfessor 10..* teaches
Association Classes can have role names (nouns) Typically represent attribute name in other class CourseProfessor teacher Class Course { teacher: Professor; … } Class Professor { myCourse: Course; … }
Association Associations can be reflexive In this case role names are required Professor mentor mentee
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 o To prohibit access use TextBookCourse
Association Class A class that represents the association CourseStudent 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 ProfessorDepartment
Composition A stronger form of aggregation One class contains objects from the other class These objects have no existence without the containing object CourseProgram 1
AggregationComposition Relationshiphas-awhole-parts Container typereferencesobjects Lifecycleelements are independent elements are created with container and die with container Elementscan be in multiple aggregations can be in only one composition
Dependency One class uses another o Local in method o Argument to method o Creation of objects (e.g. factory) But it is not an attribute (field) Usually many; show only important ones CA method(arg : B) B «creates»
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 addStudent() Student {enrolment capacity} Only nice ones
Object Diagrams Similar to class diagrams Show object instances rather than classes Reflect relationships at runtime No reflexive associations o ListNode class: next is reflexive o ListNode object: next is reference to another object UML Class Diagrams