0% found this document useful (0 votes)
176 views

Lesson 2 - Class Diagrams

The document discusses class diagrams in UML including what a class is, how classes are visualized, attributes, operations, relationships between classes like association, aggregation, composition, generalization, stereotypes, and roles classes can play like boundary classes.

Uploaded by

kasun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views

Lesson 2 - Class Diagrams

The document discusses class diagrams in UML including what a class is, how classes are visualized, attributes, operations, relationships between classes like association, aggregation, composition, generalization, stereotypes, and roles classes can play like boundary classes.

Uploaded by

kasun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Lesson 2:

Class Diagrams
Diagrams in UML
• A diagram is the graphical presentation of a set of elements, most often
rendered as a connected graph of vertices (things) and arch (relationship)
• UML includes nine (9) diagrams

Static Diagrams Dynamic Diagrams


Class Diagram Use Case Diagram
Object Diagram Sequence Diagram
Component Diagram Collaboration Diagram
Deployment Diagram State Diagrams
Activity Diagrams
2
4 + 1 View of the UML
UML consists of 5 views and it covers all the stages of System Development Life Cycle (SDLC),

3
What is a class?
• A Class is a description of a group of objects with common
properties (attributes), common behavior (operations), common
relationships to other objects, and common semantics.
Class Diagrams
• Backbone of nearly all OO Methods.
• A class diagram describes the types of objects in the system
and the various kinds of static relationships that exist among
them.
• It also shows the attributes and services of a class and the
constraints that apply to the way objects are connected.

Book
Accno
Title
UML Notation for a Class Author
GetAuthor(int)
Visualizing a Class

• Represented by a rectangle
• Class naming
– represented by a word with an initial uppercase letter.
– Appears near the top of the rectangle
– If the class has two word name, join the two words together and
capitalize the first letter of the second word.
Attributes

• A property of a class
• Describes a range of values that the property may hold in objects of
that class.
• A class may have zero or more attributes.
• If name consists of more than one word the words are jointed and
each word begins with a uppercase letter.
Operations
• An operation is some action that one object performs upon another
in order to response a reaction.
• A class may have more than one operation or no operations.
• The operation name should always starts with a
simple letter and
• If the operation name consists of more than one word,
then all the words should be combined by capitalizing
the first letter of every word except the first word.
Activity
Visualize any class you know and indicate attributes and operations of
that class
Working with Relationships
The Need for Relationships
• Relationships provide the conduct for object interactions.
• A relationship is a semantic connection between classes.
• It allows one class to know about the attributes, operations of another
class.
• In object oriented analysis and design there are four main types of
relationships.
• Those are generalization, dependency, realization and association.
• Aggregation and Composition are special types of association
relationships.
Dependency
• Class A depends on Class B, if any change to a class B forces a change to
a Class A. i.e. any operation occurred in class B changes the states, or the
values of the attributes of class A.
• In UML, the dependency is rendered using a dashed directed line,
which directed to the things or the class being depended on.
• For example, if Class A depends on B, then it can be represented as in
below figure
Dependency
• For example, consider the Window Class and Event Class
• The double click event on window icon will maximize the window.
• Therefore, operations fired in the Event class changes the status of
Window Class.
• Therefore, we can say, Window class uses the Event class, or Window
class depends on Event class
Association

• Indicate a connection (a link) between classes.


• Each class can send messages to the other.
• It can be bi-directional or unidirectional.
• In UML, bi-directional associations are drawn either with arrowheads
on both ends or without arrowheads altogether.
• Association can have name, role and multiplicity
Association - Name
• Association can have name and it is used to describe the nature of the
relationship.
• To make it more clear about the relationship, in addition to the name,
direction can also be given to specify the direction you intended to
read the name.
• Usually direction is rendered by direction triangle.
• Following figure shows the association between Employee and
Department.
Associations - Role
• A role denotes the purpose or capacity wherein one class associates
with another.
• Role names are typically nouns.
• A role name is placed along the association line close to the class it
modifies.
• One or both ends of an association may have role names.
• You can show both association names and role names.
• Below figure shows the role played by the employee on the manage
association.
Recursive Associations
• Below figure shows the recursive association, where Employee plays
the role of supervisor on the supervised by association.
Associations
• Multiplicity
– The number of objects from one class that relate with a single
object in an associated class.
– Multiplicity can be denoted near the appropriate class
Multiplicity

1 Exactly one
0..* Zero or more
1..* One or more
0..1 Zero or one
5..8 Specific Range (5,6,7 or 8)
Activity 1
Department is managed by exactly one Head of the Department (HOD). And each
HOD manages only one department.

Activity 2
In the recursive association, let assume that one or more employees are supervised
by exactly supervisor. And each employee can have only one supervisor.
Activity 3
A student in an institute follows one or more courses and a particular course is
followed by at least twenty or more students.
Aggregation
• Aggregation is a specialized form of association in which a whole is related
to its part(s).
• This is also known as whole-part relationship or has-a relationship,
meaning that an object of the whole has object of the part.
• Aggregation is typically rendered as an association with an open diamond
(empty) at the whole end.
• Example: Each honours course requires/has six or more module. And a
module may offer to more than one course

1..* 1..*
Aircraft Engine
Composition
• Composition is a special kind of aggregation.
• The whole strongly owns it’s parts.
• If the whole object is copied or deleted, then the parts are copied or
deleted with it.
• Multiplicity at whole end must be 1.
• Composition is rendered by filled diamond at the whole end.
• E.g. A faculty has one or more department. A department belongs to only
one faculty.
1 1..*
School Department
Generalization
• Parent class is a generalization of the child class, i.e. generalization is a
relationship between a parent (known as super‐class) and a child
(known as sub‐class).
• This relationship also known as ‘is a’ or ‘is a kind of’ relationship.
• The relationships such as Teacher is a Person, Rose is a Flower are
examples for generalization.
• A class can have zero, one or more parents.
• In Java, a class is allowed to have only one parent.
• A class which has no parents but has one or more children is known
Base Class.
• And a class with no children is known as a Leaf Class.
Generalization
• In UML, the generalization relationship is rendered as a sold directed
line with a large open arrowhead pointing to the parent class.
• For example, we can render the Teacher is a Person relationship as
shown in below figure.
Inheritance (Generalization)
• An inheritance relationship:
– is not a relationship between different objects.
– is a relationship between different classes.
– Rolenames are not used.
– Multiplicity does not apply.
Abstract Class
• Intended only as bases for inheritance
• Provides no objects of their own
• Indicated by writing the name of the abstract class in italics
e.g.
Stereotypes and Classes
A Stereotype is a mechanism you can use to categorize your classes.
– Say you want to quickly find all of the forms in the model,
– You could create a stereotype called form, and assign all of your
windows this stereotype.
– To find your forms later, you would just need to look for the classes
with that stereotype.
Stereotypes and Classes
• There are three primary class stereotypes in UML.
Stereotypes and Classes

Boundary Class:

• They provide the interface to a user or another system. (ie. Interface to


an actor).
• Handles communication between system surroundings and the
inside of the system.
• To find the Boundary classes, you can examine your Use Case diagram,
Stereotypes and Classes

Boundary Class:

• At a minimum there must be, one Boundary class for every actor-
use case interaction.
• Boundary class allows actor to interact with the system.

Actor 1 Use Case 1


Boundary Class
Stereotypes and Classes
Boundary Class:
• You do not necessarily have to create a unique Boundary
class for every actor-use case pair.
• Two actors may initiate the same use case.
• They might both use the same Boundary class to communicate
with the system.
Boundary Class
Actor 1

Actor 2 Use Case 1


Stereotypes and Classes

Entity Class

• They are needed to perform task internal to the system.


• Reflect a real world entity.

Identifying Entity Classes


• Identify the nouns and noun phrases used to describe the
responsibilities.
Stereotypes and Classes

Entity Class

• The initial list of nouns must be filtered because,


– it could contain nouns that are outside the problem domain.
– nouns that are just language expressions.
– nouns that are redundant.
– nouns that are attributes.
Stereotypes and Classes

Control Class:

• Sequencing behaviour specific to one or more use cases.


• There is typically one control class per use case.
• Co-ordinates the events needed to realise the behaviour
specified in the use case.
e.g. Running or executing the use case.
Question 1
• A company consists of departments. Departments are located in one
or more offices. One office acts as a headquarter. Each department
has a manager who is recruited from the set of employees. Your task
is to model the system for the company.

Task:
• Draw a class diagram which consists of all the classes in your system
their attributes and operations, relationships between the classes,
multiplicity specifications, and other model elements that you find
appropriate.
Object Diagrams
Object Diagrams

• Object diagrams are sometimes called as instance diagrams which


are useful for representing real world examples of objects and the
relationship between them.
• Although UML class diagrams are very good at describing this
information at certain situation the class diagram is to abstract
describing very high level information.
• An object can also be thought of as the description of an individual
within a group while classes describe that characteristic shared by
the group.
Object Diagrams
• An object diagram focuses on some particular set of objects instances and
the links between the instances.
• As associated set of object diagrams provides inside into how an arbitrary
view of a system is expected to involve overtime.
• Object diagrams are more concrete than class diagram and are often use
to provide examples or even act as text cases for the class diagram.
• Although, we define and design classes in a real world application, classes
are not implemented directly to represent the logic where as object
diagrams are used to represent the business logic.
Object Diagrams

• The above figure shows a simple class representing an Employee and an


instance of the Employee class called Sarath.
• As you can see, the instance contains values for the attributes that are
defined in the class.
• One way of naming the object.
• The object name, Sarath, and the class name Employee connected by a
colon (:), i.e. Sarath:Employee
Object Diagrams

• In some cases it is possible to omit the object name


and only use the :ClassName notation.
• In such a case we say that we are representing an anonymous
object belonging to the class.
• This allows the representation of objects without introducing
unnecessarily long names that clutter up diagrams.
• Anonymous objects also allow representation of general items that
are valid for many objects of the same type.
• Figure 8.2 shows three different ways of naming objects.
Object Diagrams

• It is also possible to use a fully qualified name using package names


separated by colons:
Object Diagrams
Associations
• Links are the relationships between objects.
• Just as objects are instances of classes, links can be considered as
instances of associations.
• It is easier to understand the representation of a structure when
connections between objects are used than when the abstract
representation of classes are used.
• This is especially clear when we consider recursive relationships.
• For example, the manager of an Employee is an Employee himself can
be structurally represented as follows:
Associations
Object Diagram
• An object diagram represents the complete or a partial occurrence of
instances of classes or objects and their links at a given point of time.
• For example, let us take a simple example of Employees,
Departments and the association between these.
• Figure We could represent an Employee class, a Department class
and the association “manages” between an Employee and a
Department as follows.
• For simplicity’s sake we will only represent a few attributes for each
class.
Object Diagrams
Use of Object Diagram and its application Class

• Object diagrams are a useful tool to look at the situation being


modeled as a concrete view.
• This is because objects are concrete entities whereas classes show only
an abstract view of objects.
• A set of object diagrams that shows the same sub-set of a class diagram
could easily allow us to see how a system evolves over time.
• Object diagrams are also often used as examples to explain class
diagrams.
Use of Object Diagram and its application Class

• Since object diagrams contain actual objects instead of an abstract


representation, they are easier to relate to the real-world scenario.
• Object diagrams can also be used as test cases to validate that a class
diagram is a faithful representation of the real-world situation being
modeled
Question 2
Draw a UML Class Diagram representing the following

• Elements from the problem domain for a hockey league. A hockey league is made
up of at least four hockey teams. Each hockey team is composed of six to twelve
players, and one player captains the team. A team has a name and a record.
Players have a number and a position. Hockey teams play games against each
other. Each game has a score and a location. Teams are sometimes lead by a
coach. A coach has a level of accreditation and a number of years of experience,
and can coach multiple teams. Coaches and players are people, and people have
names and addresses. Draw a class diagram for this information, and be sure to
label all associations with appropriate multiplicities.

You might also like