Professional Documents
Culture Documents
Part3: Class Diagram
Part3: Class Diagram
Classes
attributes
operations
Class Attributes
Person
• Visibility
• Public[+].
• Protected[#].
• Private[-].
Toolbar
public #currentSelection protected
#toolCurrent
+pickItem()
+addTool()
+removeTool()
protected +getTool()
#checkOrphans()
private -compact()
5
Visibility
• Public[+]: any outside classifier with visibility to the given classifier can use this feature.
Visibility
• Protected[#]: attributes and operations are specified using the hash (#) symbol and are more visible to
the rest of your system than private attributes and operations, but are less visible than public.
Declared protected elements on classes can be accessed by methods that are part of your class and
also by methods that are declared on any class that inherits from your class
Visibility
• Private[-]: only the classifier itself can use the feature.
Class Operations
Person
name : String
address : Address
birthdate : Date
ssn : Id
Person Person
Person
name : String
birthdate : Date
Person ssn : Id
Dependencies
Generalizations
Associations
Dependency Relationships
A dependency indicates a semantic relationship between two or
more elements. The dependency from CourseSchedule to Course exists because
Course is used in both the add and remove operations of CourseSchedule.
CourseSchedule
Course
add(c : Course)
remove(c : Course)
Generalization Relationships
Person
A generalization connects a subclass to its superclass.
Student
Student Employee
TeachingAssistant
Regular Loyalty
Customer Customer
Subtype Subtype2
1 or: Customer
Generalization expresses a
relationship among related
classes. It is a class that
includes its subclasses.
Regular Loyalty
Customer Customer
Association Relationships
If two classes in a model need to communicate with each other, there must be link
between them.
Student Instructor
Association Relationships
Two classes can relate to each other with a line and an association name.
Association: Multiplicity
Multiplicity allows us to indicate how many objects of one class relate to one object of another
class. We can add multiplicity on either end of a class relationship by simply indicating it next to
the class where the relationship enters.
Multiplicity
• It specifies the number of instances of one class that may relate to a single
instance of the associated class.
• UML diagrams explicitly list multiplicity at the end of association lines.
Multiplicity
Symbol Meaning
1 One and only one
0..1 Zero or one
M..N From M to N (natural language)
* From zero to any positive integer
0..* From zero to any positive integer
1..* From one to any positive integer
Association: Multiplicity and Roles
Role
“A given university groups many people; some act as students, others as
teachers. A given student belongs to a single university; a given teacher may or
may not be working for the university at a particular time.”
Association: Multiplicity and Roles
Here we see that a Student takes 4 to 6 classes, and a Class has 10 to 30 students.
Association: Multiplicity and Roles
Our class relationships can be further beefed up by adding roles. The use of a role in a class diagram helps
the reader to understand what the first class does for the second.
A role is shown in the same place as the multiplicity, either above or below the line indicating the relationship
where it enters the class.
“ClassOne plays the role of rolename1 for ClassTwo” and “ClassTwo plays the role of rolename2 for ClassOne.”
Association: Multiplicity and Roles
From this class diagram, we can deduct that “A Class has one Teacher who plays the role of the
Instructor” and “A Class has zero or one Teacher who plays the role of the Assistant.”
Association: Multiplicity and Roles
We can indicate the multiplicity of an association by adding multiplicity adornments
to the line denoting the association.
Student Instructor
1..*
The example indicates that every Instructor has one or more Students:
Student Instructor
1..*
Association: Multiplicity and Roles
We can also indicate the behavior of an object in an association (i.e., the role of an
object) using rolenames.
member of
1..* 1..*
Student Team
1 1..*
president of
Association: Aggregations
We can model objects that contain other objects by way of special associations called
aggregations and compositions.
Engine
Car
Transmission
Association: Aggregations
Association: Aggregations
Container Class
Class C
Aggregation implies a relationship where the child can
AGGREGATION
exist independently of the parent.
Class E1 Class E2
Example: Class (parent) and Student (child). Delete the
Class and the Students still exist.
Containee Classes
Scrollbar
1 1
Window Titlebar
1 1
Menu
1 1 .. *
Association: composition
COMPOSITION
Whole Class Composition implies a relationship where the child cannot
Class W
exist independent of the parent.
Example: House (parent) and Room (child). Rooms don't exist
separate to a House.
Class P1 Class P2
open() Event
close()
dependency
generalization
association
40
Generalization
• Relationship between general thing (parent) and more specific thing
(child)
• Child “is-a-kind-of” parent.
• Child inherits attributes and operations of parent.
AudioClip
name
Microphone
record(m:Microphone)
start()
stop()
dependency
Associations (UML)
Represent conceptual relationships between classes
direction indicator:
how to read relation name
relationship name
association class
class association name
Worker 1..* works for* Company
employee employer
+setSalary( s : Salary)
+setDept( d : Dept)
w : Worker assign(develop
: Company
ment)
Named object link Anonymous object
Associations - Aggregation
- structural association representing “whole/part” relationship.
- “has-a” relationship.
part whole
multiplicity
1..*
Department Company
association
aggregation
45
More Examples on Multiplicity
An association shows how classes are connected to each other:
– Symbols indicating multiplicity are shown at each end of the association.
*
Employee Company
* 1..*
Secretary Manager
Company BoardofDirectors
0..1 *
Office Employee
0, 3..8 *
Person BoardofDirectors
LEGEND
x..* (the range from x to many).
x..y (the range from x to y).
4
7
Each association can be labelled, to make explicit
the nature of the association:
Association Association
Role Association Name Role
Class1 Class2
4
8
* Works for →
Employee Company
* has 1..*
Secretary Manager
Company BoardofDirectors
0..1 isAllocated *
Office Employee
0, 3..8 isMember *
Person BoardofDirectors
4
9
Analyzing & Validating Associations
* Works for →
Employee Company
Many-to-one
Many-to-many
– A secretary can work for many managers.
– A manager can have many secretaries.
– Secretaries can work in pools.
– Managers can have a group of secretaries.
– Some managers might have zero secretaries.
– It is not possible for a secretary to have zero managers.
9
ANALYZING & VALIDATING ASSOCIATIONS
Company BoardofDirectors
One-to-one
0, 3..8 isBoardMember *
Person BoardofDirectors
Many-to-many
Passenger * Booking
54
Ex: Passenger Reservation System (2)
Booking * SpecificFlight
55