Class Diagram Basics in OOP
Class Diagram Basics in OOP
WHAT IS CLASS?
A class describes a group of objects with
similar properties (attributes),
common behavior (operations),
common relationships to other objects,
and common meaning (“semantics”). Student
Name Name (mandatory)
Attributes
(optional) ID #
Examples Department
Employee: Registration()
Assignment() Operations
- has a name, employee id, and department;
Exam Result() (optional)
- an employee works in one or more projects
Object Oriented Analysis and Design: Class Diagram
S.3
CLASS DIAGRAM
The Class diagram represents classes, their component parts, and the way in
which classes of objects are related to one another.
The Class diagram includes:
Attributes describe the appearance and knowledge of a class of objects (e.g.,
birds have wings).
Operations define the behavior that a class of objects can manifest (e.g., dog
is barking).
Stereotypes help you understand this type of object in the context of other
classes of objects with similar roles within the system’s design.
Properties provide a way to track the maintenance and status of the class
definition
(e.g., properties of a class attribute can be public/private/protected)
Classes are used to represent objects. Objects can be anything having
properties and responsibility.
Object Oriented Analysis and Design: Class Diagram
S.4
Name Compartment
Attribute Compartment
visibility / attribute name :
data type
= default value
{constraints}
Operation Compartment
visibility operationName
( argname : data type
{constraints}, ...) : return data
type {constraints}
Object Oriented Analysis and Design: Class S.
12
Works for
Employee Company
An association between two classes indicates that objects at one end of an
association “recognize” objects at the other end and may send messages to
them.
This property will help us discover less trivial associations using interaction
diagrams.
Qualified association in class diagram indicates a qualifier to be used for
Indexing
Object Oriented Analysis and Design: Class S.
14
ASSOCIATIONS
Associations may optionally have the following:
Association name
Small black arrowhead to indicate the direction in which the name should
be read
Should be a verb or verb phrase;
Role names
on one or both association ends;
* *
should be a noun or noun phrase describing the semantics of the role;
Multiplicity
The number of objects that can participate in an instantiated relation
Navigability
The direction of Arrows
Object Oriented Analysis and Design: Class S.
15
ASSOCIATION
Association multiplicity MULTIPLICITY
limits the number of objects that can participate in an
association.
Provides a lower and upper bound on the number of instances (at opposite end)
Indicates whether an association is mandatory
MULTIPLICITY INDICATOR
Exactly one 1
Many *
Zero or more 0..*
One or more 1..*
Zero or one (optional association) 0..1
Specified range 2..4
Multiple, disjoint ranges 2, 4..6, 8
Object Oriented Analysis and Design: Class S.
17
AGGREGATION
A special form of association that models a whole- part relationship between
an aggregate (the whole) and its parts (shared class i.e., Part by Whole
class)
Models a “is a part-part of” “has-a” relationship.
2..* 1..*
Car Door House
AGGREGATION
Is the phrase “part of” used to describe the relationship?
A door is “part of” a car
Are some attribute values propagated from the whole to all or some of its
parts?
The car is blue; therefore, the door is blue.
COMPOSITION
ASSOCIATION CLASS
REFLEXIVE ASSOCIATION
Reflexive association is a fancy expression that says objects in the same
class can be related to one another. The entire association notation you’ve
learned so far remains exactly the same, except that both ends of the
association line point to the same class.
Object Oriented Analysis and Design: Class S.
23
QUALIFIED ASSOCIATION
Object Oriented Analysis and Design: Class S.
24
GENERALIZATION
Generalization is a relationship between a more general thing and a more
specific thing:
The more specific thing is consistent in every way with the more general
thing.
The substitutability principle states that you can substitute the more
specific thing anywhere the more general thing is expected.
Generalization is a bottom-up process
Generalization hierarchies may be created by generalizing from specific
things or by specializing from general things.
“is kind of” or “is type of” relationship.
Object Oriented Analysis and Design: Class S.
25
GENERALIZATION
Shape Super
A class
Class
Generalization
relationship
Circle Sub Class
Object Oriented Analysis and Design: Class S.
26
INHERITANCE
A sub-class inherits from its super-class
Attributes
Operations
Relationships
A sub-class may
class A2 extends B2
Add attributes and operations
Add relationships
Refine (override) inherited operations
REALIZATION
A realization relationship indicates that one class implements a behavior
specified by
another class (an interface).
An interface can be realized by many classes. In another words, a class
may realize many
interfaces.
class A3 implements B3
Object Oriented Analysis and Design: Class S.
28
DEPENDENCY
A dependency indicates a semantic relation between two or more classes
in which a change in one may force changes in the other.
Customer 1 * may be
Order canceled
{ total < $50 }
id: long { value > 0 }
Note
Constraint
Object Oriented Analysis and Design: Class S.
30
b a
b.a a.A
b.a.F
b.b
b.a.G
a.C
Object Oriented Analysis and Design: Class S.
31
CLASSES
Boundary Classes CATEGORIZATION
Models the interaction between the system’s surroundings and its inner
workings
User interface classes, Concentrate on what information is presented to
the user,
don’t concentrate on user interface details
System / Device interface classes, concentrate on what protocols must be
defined.
don’t concentrate on how the protocols are implemented
Entity Classes
Models the key concepts of the system
Usually models information that is persistent
Contains the logic that solves the system problem
Can be used in multiple behaviors
Object Oriented Analysis and Design: Class S.
32
CLASSES CATEGORIZATION
Control Classes
Controls and coordinates the behavior of the system
A control class should tell other classes to do something and should never
do anything except for delegating (directing) the work to other classes
Control classes separate boundary and entity classes
Object Oriented Analysis and Design: Class S.
33
CLASSES CATEGORIZATION
ReportDetailsForm 1 Of f endersDBProxy
<<boundary >> 1 <<boundary >>
Of f endersDB
EditReportController
<<control>>
Clerk
CRC CARD
Class:
Class:
Description:
Class:
Description:
Class:FloorPlan
Description:
Responsibility:
Description: Collaborator:
Responsibility: Collaborator:
Responsibility: Collaborator:
Responsibility: Collaborator:
defines floor plan name/type
manages floor plan positioning
scales floor plan for display
scales floor plan for display
incorporates walls, doors and windows Wall
shows position of video cameras Camera
Object Oriented Analysis and Design: Class S.
35
CRC CARD
A CRC card is a 3-x-5" or 4-x-6" lined index card.
The physical nature of the cards emphasizes the division of responsibility
across objects.
The physical size of the cards also helps to establish limits for the size and
complexity of the classes.
The CRC card technique does not use the UML, instead it is used to
discover information about classes that is then placed into a UML Class
diagram.
The body of the card is divided in half.
The left column/half lists the responsibilities of the class
The right column/half lists the other objects that it works with, the
collaborators,
to fulfill each responsibility.
Object Oriented Analysis and Design: Class S.
36
REFERENCES
Booch, G., Rumbaugh, J. & Jacobson, I. (2005). The unified modeling language user
guide. Pearson Education India.
Object Oriented Analysis and Design: Class S.
38
CASE STUDIES
Case 1: Draw the Class notation for Class Named ‘Book’.
Private attributes of the class are:
bookID ->Numeric; assigned by system, default value: none,
bookName. ->String,
authorName->String ,
bookCopy->Numeric,
totalBook->Static and numeric
Public methods of this class are:
setBookID(Numeric; assigned by system, default value: none),
setBookName(String),
setAuthorName(String) ,
addBookCopy(Numeric ,default value: zero),
deleteBookCopy(Numeric ),
showBookInfo (void)
Object Oriented Analysis and Design: Class S.
39
CASE STUDIES
Case 2: Draw the Class notation for Class Named ‘Movie’.
A movie is a collection of visual pictures that tell a story and are displayed on a screen, generally with
sound. A film producer produces movies in a different category. Every movie category has some
attributes. For example, MovieID, Name, ReleaseDate, Rating, DurationTime, Actor and Actress.
All attributes are private. DurationTime and Rating is Double in data type. MovieID is an integer
and other attributes are String in data type. There might be six methods for almost every movie
category. All methods are public. They are: setScriptTitle(String Title), setMovieName(String
Name), getReleaseDate(). All methods return String value without getReleaseYear(). Only
getReleaseYear() methods return an integer value.
Object Oriented Analysis and Design: Class S.
40
CASE STUDIES
Case 3: Draw the Class diagram for the following scenario.
Student may attend any number of courses
Every course may have any number of students
Instructors teach courses
For every course there is at least one instructor
Every instructor may teach zero or more courses
A school has zero or more students
Each student may be a registered member of one or more school
A school has one or more departments
Each department belongs to exactly one school
Every instructor is assigned to one or more departments
Each department has one or more instructors
For every department there is exactly one instructor acting as the department chair
Object Oriented Analysis and Design: Class S.
41
CASE STUDIES
Case 4: Draw the Class diagram for the following scenario.
CASE STUDIES
Case 5: Draw the Class diagram for the following scenario.
CASE STUDIES
Case 6: Draw the Class diagram for the following scenario.
The library contains books and journals. It may have several copies of a given book. Some of
the books can be reserved for short-term loans and others for long-term. Member of library
can borrow books. There is a special type of member- Member of Staff, who can borrow the
journals. The system must keep track of when books and journals are borrowed.
Object Oriented Analysis and Design: Class S.
44
CASE STUDIES
Case 7: Draw the Class diagram for the following scenario.