Class Diagram and Relationship
Class Diagram and Relationship
Classes
A class is a description of a set of
ClassName objects that share the same attributes,
operations, relationships, and semantics.
attributes
Graphically, a class is rendered as a
rectangle, usually including its name,
operations attributes, and operations in separate,
designated compartments.
Class Names
The name of the class is the only required
ClassName tag in the graphical representation of a
class. It always appears in the top-most
attributes compartment.
operations
Class Attributes
Person
/ age : Date
Class Attributes (Cont’d)
Person
Person
name : String
address : Address
birthdate : Date
ssn : Id
eat Operations describe the class behavior
sleep and appear in the third compartment.
work
play
Class Operations (Cont’d)
PhoneBook
display()
Operations hide()
10
Class Names
• Name must be unique within its enclosing package
• Simple name and Path name
• Class names are noun phrases from the vocabulary of the
system
Class Responsibilities
• A class may also include its responsibilities in a class
diagram.
• A responsibility is a contract or obligation of a class to
perform a particular service.
• Attributes and operations are the features that carry out the
class’s responsibility
SmokeAlarm
Responsibilities
• dependencies
• generalizations
• associations
Dependency Relationships
A dependency indicates a semantic relationship between two or
more elements.
It is a special type of association (using) exists between two
classes, if changes to the definition of one may cause changes to
the other (but not the other way around).
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)
Dependency Relationships
Dependency Relationships
Student Employee
TeachingAssistant
Generalization
• A sub-class inherits from its super-class
– Attributes
– Operations
– Relationships
• A sub-class may
– Add attributes and operations
– Add relationships
– Refine (override) inherited operations
22
Modeling Single Inheritance
• Find classes that are structurally or behaviorally similar while modeling the
vocabulary
• To model inheritance
– Look for common responsibilities
– Elevate these to a more general class
– Specify that the more specific class inherits from the more general class
Association Relationships
Associations are relationships between classes in a UML Class
Diagram. They are represented by a solid line (Bi-directional)
between classes.
Student Instructor
Association : Adornments
• Name
– Use a name to describe the nature of
the relationship
– Can give a direction to the name
Association : Adornments
• Role
– The face that the class at the far end of the association presents
to the class at the near end
– Explicitly name the role a class plays (End name or Role name)
– Same class can play the same or different roles in other
associations
Association: Adornments
• Multipicity
Student Instructor
1..*
Some typical examples of multiplicity:
An expression that evaluates to a range of values or an explicit
value
Multiplicity Cardinality
Student Instructor
1..*
Multiplicity
– Specify complex multiplicities by using a list, 0..1, 3..4, 6..* (any number of
objects other than 2 or 5)
Association Relationships (Unidirectional)
Router DomainNameServer
Association Relationships (Cont’d)
Associations can also be objects themselves, called link classes
or an association classes.
Registration
modelNumber
serialNumber
warrentyCode
Product Warranty
Association Relationships (Cont’d)
Association Relationships (Cont’d)
Engine
Car
Transmission
Aggregation
2..* 1..*
Car Door House
Scrollbar
1 1
Window Titlebar
1 1
Menu
1 1 .. *
1 Circle
Circle Point
3..* Point
Polygon
Aggregation and Composition: Key Differences
1.Existence Dependency:
1. In aggregation, the "part" class can exist independently of the "whole"
class.
2. In composition, the "part" class typically cannot exist independently; it's
dependent on the "whole" class.
2.Strength of Relationship:
1. Aggregation represents a weaker form of association, where the "part"
can be associated with multiple "wholes."
2. Composition represents a stronger form of association, indicating a
strict ownership relationship where a "part" belongs exclusively to a
single "whole."
3.Notation:
1. Aggregation is denoted by an unfilled diamond shape.
2. Composition is denoted by a filled diamond shape.
Realization
43
Example Class Diagram –
hotel management system
Example Class Diagram - ATM system