0% found this document useful (0 votes)
28 views39 pages

Object Oriented Techniques Unit 2 by Dr. Choudhary Ravi Singh

The document provides an overview of class and object diagrams in object-oriented programming, detailing their definitions, components, and differences. It explains basic terms such as classes, attributes, operations, and relationships, along with various types of associations and modeling techniques. Additionally, it covers collaboration and sequence diagrams, emphasizing their roles in illustrating object interactions and system behavior.

Uploaded by

ravi singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views39 pages

Object Oriented Techniques Unit 2 by Dr. Choudhary Ravi Singh

The document provides an overview of class and object diagrams in object-oriented programming, detailing their definitions, components, and differences. It explains basic terms such as classes, attributes, operations, and relationships, along with various types of associations and modeling techniques. Additionally, it covers collaboration and sequence diagrams, emphasizing their roles in illustrating object interactions and system behavior.

Uploaded by

ravi singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Ch. Ravi Singh, RBCET BLY, CS Dept.

Class Diagrams:
A class describes the group of objects with similar properties (attributes), common
behaviour (operations), common relationship to other objects and common semantics.

In object-oriented programming, a class is a construct that is used to define a distinct


type.

“In object-oriented programming, a class is a template definition of the methods and


variables in a particular kind of object. Thus, an object is a specific instance of a class”.

“A set or category of things having some property or attribute in common and


differentiated from others by kind, type, or quality”.

Class diagram is a graphically representation used for modelling classes and their
relationships.

“The class diagram is used to model the static design view of a system. Class diagram is also
the foundation for a couple of related diagrams: component diagrams and deployment
diagrams”.
In the figure there is one class named Customer.

Customer

Name

Address

Phone

Tax()

Class diagram

Basic term and Concepts:


There are three basic terms, are used to explain the class diagram.

Name: A class name must be unique within its enclosing package. Every class must have a
name that distinguishes it from other classes.

Attributes: A class may have any number of attributes or no attributes at all. An attribute
represents some property of the thing you are modelling, that is shared by all objects of that
class.

Operations: An operation is the implementation of a service.

1
Class diagrams commonly contain the following things:
Classes, Interfaces, Collaborations, Dependency, Generalization, Association relationships.

Object diagrams/ Instance diagrams:


The object diagrams represent objects and their relationships with each other.
The Object diagrams are used to model the instances (examples) of things contained in class
diagrams. An Object diagram shows a set of objects and their relationship. Object diagram is
also used to model the static design view of a system.

Customer
Ram: Customer
Name

Address
:Customer
Phone

Salary()
Mohit

Class diagram Object diagrams


In this figure class is customer with three objects Ram (which is marked explicitly as being a
customer object), Customer (an anonymous customer object) and Mohit (A kind of customer
object although it is not shown explicitly here).

Basic terms and Concepts:


Following basic term are used to explain the object diagrams:
Name: Name of an object.
Attributes: each object contains own attribute values, representing properties of the objects.
Operation: Services provided by an object.
Link: Links are used to bind one object with other.
Anonymous object: The object without name.

Differences between Class Diagrams and Object Diagrams:-


In class diagram each class is a collection of An object is basically an instance of a class.
data and function. These functions are used
to manipulate data
The class diagram acts as a basis for the The object diagrams are created from the
object class diagrams.
A single class can create multiple objects Every object is created one and only one
class
The significance of class diagram is that, it The significance of object diagram is that,
represents the overall system model We can create real word system model using
it
Example class Vehicle Example Objects Alto, Santro are the objects
of the class vehicle

2
Link & Associations:
Link is a connection between the objects. It represents a simple association of one object
with another.
A link is a physical or conceptual connection between objects instances. A link is basically an
instance of association.
Example: John works for Simplex Company

John Works for Simplex


Company

An association is a group of links that have common structure and common semantics.

Example: A Person works for Company.

Person works for Company

Example:

Student Adopts for Course

Class Diagrams Class Diagram

Adopts for EC: Course


Sachin: Student

Adopts for CE: Course


Shilpa: Student

Object Diagrams Object Diagrams

The Link is between two objects and the association is between two classes.
Person lives in a country....................Association
Mohan lives in India.............................Link

3
Association Classes:
An association class is "A modelling element that has both association and class
properties. An association class can be seen as an association that also has class
properties, or as a class that also has association properties".

Example:
Course
Student
Course Name
Name * Enrols for *
Course- Id
Student-Id
Fee
Address
Allocate Student ()
Check Eligibility ()
Cancel Allocation ()
Select Course ()
Update Course ()

Enrollment

Marks

Cal-Avg-Marks()

Association Classes
Qualified Association:
A qualified association has a qualifier that is used to select an object (or objects) from a
larger set of related objects, based upon the qualifier key.

Course Student

Student-Id: Int

Qualified Associations
Selection of particular Student from the Course, the qualifier will be Student-Id .

4
Reflexive Association:
It occurs when a class may have multiple functions or responsibilities. For example, a staff
working in an airport may be a pilot, aviation engineer, a ticket dispatcher, a guard, or a
maintenance crew member. If the maintenance crew member is managed by the aviation
engineer there could be a managed by relationship in two instances of the same class.

Ordering:
This type of association is used to denote the set of objects at one end must appear in some
specific order.

Browser Web Pages


1 Displays *

Ordering

One web browser displays many web pages.

Bags & Sequences:


In association relationship there are some objects that may be duplicated or may appear in
some sequence. In these cases bags and sequences are used at association ends.
Bag is a collection of objects at one end that are duplicated. That means if a word bag
appear at some end then the object at that end can appear more than one.

*Person * * Jobs
{Bag}

Sequence is a collection of objects at other end that are duplicated or appear in some
sequence.

System Sends {sequence} Message

Modelling Techniques for Class and Object Diagrams:


The following modelling techniques are used for modelling class and object diagrams:
1. Modelling Comments
2. Modelling distribution of Responsibilities
3. Modelling Structural Relationship

5
4. Modelling single and multiple Inheritance.
5. Modelling Vocabulary of the System.
6. Modelling different Views of a System.
7. Forward and Reverse engineering.

Modelling Comments:
Comments are nothing but the free form observations, reviews or explanations. We can put
the comments in the Note.
If a comment is necessary then place the information in some external document and link that
document in the Note.
Discard the comments that are not important.

Modelling distribution of Responsibilities:


Identify a set of classes that work together to demonstrate some behaviour.
Identify the set of responsibilities for those classes.
Example:
Model View

Responsibilities Responsibilities
Manage modelling Display the output on
of entity screen

Controller

Distribution of responsibilities
Responsibilities
Control the 6
modelling and
viewing activities
Modelling Structural Relationship:
The structural relationship can be modelled using the association class. By default the
association relationship is bidirectional but we can limit the direction and can make it
unidirectional.
Example:

Patient 1.* has Hospital 1 has 1 Administrator

1.....* 1

has
Suffering
from

*
*
* * * *
Diseases Doctor Departments

Examines Associa-
ted with

Modelling single and multiple Inheritances:


If each class has only one parent then it shows single level inheritance.
Example:

Human

name

Address

Get address ();

Girl Boy

name name

Address Address

Get address (); Get address ();

7
If there is more than one parent for a class then it shows multiple inheritance.

Goods Train Passenger Train

Train

Modelling Vocabulary of the System:


1. Identify those things (classes) that user or implementers use to describe the problem.
2. Balance all the responsibilities among the classes.
3. Provide attribute and operations that are needed to carry out the responsibility for
each class.

Following is a set of classes drawn for online course reservation system.

Student Registration System Course Database


Name
Address
Id

Select course()
Submit form()
Reserve seat()
Cancel seat()

Accountant Registrar

In the above set of classes there are some important classes such as student,
registrar, and course database.

8
Modelling Relationships:

Q. Prepare a class diagram from the instance diagram in the figure below:

Answer:
Class Name: Country
Attribute names: (1) Country Name (2) Population (3) Major Language (4) Capital city
(5) Per capita Income, Member function: Make border with ( )
Association name: borders with
One country can have border with zero or more Countries

9
Q. Prepare a class diagram from the instance diagram given below

Answer:

Class name: Person


attribute name: (1) person's name (2) age (3) height (4) weight (5) sex
member function(method) : make_relation_with( )
Association name : related to
One person can have different types of relation with different person in a family

10
Q. Prepare a class diagram from the instance diagram of a geometrical document given
below; prepare the class diagram using aggregation and generalization.

Answer:

11
Draw a class diagram for school/university information/automation system.

School has
Department
Name
Generalization
Address
AddDept()
Total Students()
TotalStaff()

Examination Section
Teaching Dept NonTeachingDept
Name
Name
Name CalculateFees()
Addteacher()
Member of DisplayInfo()
AddSubject()
RemoveSubjcet()
has Library
Name
IssueBook()
Assigned to

Student
Name attends teaches
Phone Subject Teacher
Id Name
AttendClass() Salary
AttendExam() Teaching()
makeTeachingPlan()

Examination
Maintain
Name
Appears for
Date
Time
Status()

12
Draw a class diagram for hospital management system.

Has
Hospital Department
Name Name
Address interact phone
Phone
Person

TotalStaff()

Patient Has
Name Staff
Address
GetasTreated()

Undergoes Examine Operational staff Administrative Staff


through

Doctor
Name Nurse
Test conduct
Phone Name
Name
GiveTreatment() Phone
Cost
GiveService()

Draw a class diagram for ticket reservation system.


Draw a class diagram for online examination registration system.

13
Q. Given a figure below which represent an object diagram for a computer system for
laying out a newspaper. The system handles several pages which may contain among
other things, columns of text. The user may edit the length and width of column text,
moves it around on a page or move it to other page. Modify the object diagram to
handle this enhancement.

Answer:

User can manipulate ( manipulate is the name of the association) in three ways cut,
copy and paste .

14
Q. Prepare object diagram showing at least 10 relationships among the following object
classes. Include associations, aggregations and generalizations. Use qualified association
and show multiplicity balls in your diagrams. You need not to show the attribute or
operations. Use association name where needed.
School, playground, principal, school board, class room, book, student, teacher,
cafeteria, rest room, computer, desk, chair, ruler, door, swing.

Answer:

15
Collaboration Diagrams:
A collaboration diagram is an interaction diagram, show organisational behaviour of
objects that send and receive messages. A collaboration diagram can describe
graphically, a set of objects, relationship among those objects and the messages, that are
send and received by those objects.

A Collaboration diagram is a kind of model in UML to describe relationship between objects.

A Collaboration diagram is an interaction diagram in which relationship between


objects is defined by passing messages.

Example: Index Value Messages

1: View () 2: Select () 3: Display () B: Course


A: Student
Registration System

Link

Classes
Objects

Syntax for specifying an object of a class:


Object Name: Class Name

Syntax for specifying the method of a class:


Object Name: Class Name. Method Name ()

Basic things and concepts for describing Collaboration diagrams:


Link: Relationship between two objects. Links are used to build two objects.
Object: Real instances (entities) of a class.
Path Stereotype: It denotes that the objects are linked in local or global ways.
Message: Message is communication between two objects prefixed by an index value.
“A message can invoke a method, transmit a signal, or create an object”.
Index Value: It denotes the order in which messages are executed.

16
Example of a collaboration diagram:

<<Self>>

In the example of the notation for a collaboration diagram, objects are represented by rectangle
icons. Messages passed between objects are represented by labeled arrows that start with the
sending object and end with the receiving object. Where the numerical prefix to the message name
indicates its order in the sequence.

Depicting a Message in a Collaboration Diagram:


1. A message between two objects represents a communication.
2. Each message is prefixed by an index value, represents the order in which it will be
executed.
3. “A message can invoke a method, transfer a signal, or create an object”.
4. An arrow shows the direction of a message.

1: Question
A: Student B: Student

2: Answer

Polymorphism in Collaboration Diagrams:


Polymorphism is a mechanism in which the operations (methods) have the same name but
different behaviours in different environments.
Polymorphism in collaboration diagrams is shown in figure given below:

Show ()
: Canvas : Shape

17
Show () Show ()

Dao Dom
Circle: Shape : Area Triangle: Shape : Area

Use of self in Collaboration Diagrams:


Self is a keyword used to represent self message in a collaboration diagrams.
This allows the object sending message to either:
1. Sending message to itself.
Check Balance ()
Bank

<<Self>
>
2. Passing the argument self and thereby telling the target object which objects
sends the message.

A: Class1 <<Self: A: Class1>> B: Class2

Iterated Messages in Collaboration Diagrams:


Iterated message is a kind of message that is sent repeatedly. The iterated message appears in
collaboration diagrams with following peculiarities. This relationship typically exists in
aggregate object.
The iterated is prefix by asterisk * which signifies the multiplicity.
Example:
1*[i=1.....n]cnt=NextInt ()
C: Counter D: Display

18
Sequence Diagram:
A sequence diagram is an interaction diagrams that describe interactions among objects in
terms of an exchange of messages over time.
UML sequence diagrams are used to show how objects interact in a given situation. An
important characteristic of a sequence diagram is that time passes from top to bottom: the
interaction starts near the top of the diagram and ends at the bottom.

Initiator

Lifelines
Messages

Activation

Sequence diagram of email message sequence


1. The objects or roles that take part in the interaction are placed at the top of the
sequence diagram that are placed horizontally.
2. The object that starts the interaction is placed at the left and the subordinate
objects are placed to the right.
3. Activation:
Activation boxes represent the time an object needs to complete a task.

19
4. Message:
Messages are arrows that represent communication between objects. Use half-arrowed
lines to represent asynchronous messages. Asynchronous messages are sent from an
object that will not wait for a response from the receiver before continuing its tasks.

5. Lifelines:
Lifelines are vertical dashed lines that indicate the object's presence over time.

There are two types of messaging in sequence diagram:


1. Synchronous Message: Synchronous message passing systems require the sender
and receiver to wait for each other to transfer the message. That is, the sender will not
continue until the receiver has received the message.
1.1 In synchronous message only one object can send a message at a time
1.2 The sender object must wait for the target object to process the message.

20
“A synchronous message is represented by full arrowhead in sequence diagram”.

2. Asynchronous Message: Asynchronous message passing systems deliver a message from


sender to receiver, without waiting for the receiver to be ready.
“Asynchronous message is a messaging between two objects when sender object
does not wait for the target object to finish its execution of the message”.

“Asynchronous message is represented by a half arrowhead in a sequence diagram”.

Call-back Mechanism:
Call back message is a kind of that returns back to the receiving object.
Example: Request

Obj1: Class1 Obj2: Class2

Response
Call -back Mechanism

In order to understand call back mechanism refer the following scenario-


1. Client wants to purchase some items from the Retailer. Hence the Client places the
order to Retailer.
2. The Retailer when see the order, he checks for its availability. If the required items
are not available, then the Retailer places the order to the Manufacturer.
3. Manufacturer sends the items to the Retailer and then Retailer send the items to the
Client.

C: Client R: Retailer M: Manufacturer


. .
.
. .
Place Order .
.
.
.
Forward Order
.
.
. Gets Items .

. .
Supplies Order
. .

21
Asynchronous Message with Priority:
1. Due to due to concurrency in messaging the target object receives multiple messages
from multiple concurrently executing sending objects.
2. These messages need to be buffered at some place. This buffer where the messages
wait for getting executed is called message queue.
3. The target object removes the message from the message queue from its front end.
4. If the message queue is overflow then the next incoming message will be rejected.
5. The message in message queue may be ordered by priority.

High Priority
msg

Target
Medium Priority msg msg msg Object

Low Priority msg msg msg msg Front End

Message Buffer

Display Web Page ()


{Priority=2}
C: User W: Web Browser

Asynchronous message with priority

Broadcasting Messages:
In some situations sender sends the message to multiple objects at a time. This sending
of messages to multiple target objects is called broadcasting of message.
1. In broadcasting, a copy of message goes into a queue of every object in the
system.
2. The stereotype <<broadcast>> is used to represent the broadcast message.
3. When the target object receives the broadcast message, it may ignore this
message if the operation that processes this message is not available.
Example: Company may broadcast salary deposition notice to employees.

Salary Deposition Notice ()

C: Company : Employee
<<Broadcast>>

22
This diagram shows
many employees
Use Cases:
“Use cases are services or functions provided by the system to its users”.

Use Case Diagrams:


A use case diagram is a graphic depiction of the interactions among the elements of a
system.
Use case diagrams are usually referred to as behaviour diagrams used to describe a
set of actions (use cases) that some system or systems can perform in collaboration
with one or more external users of the system (actors).

Use case diagrams contains following things:

Subject: The Subject denotes the purpose of the use case diagram. It is written as a text
within a rectangular boundary. The boundary is known as system boundary.
Use Cases: The set of ellipse within which some behaviour is specified.

Actors: Actors are the users of a system.

Relationships: Illustrate relationships between an actor and a use case with a simple
line. For relationships among use cases, use arrows labelled either "include" or
"extends” or “generalization”. A "include" relationship indicates that one use case is
needed by another in order to perform a task. Base use case is dependent on the included use
case(s); without it the base use case is incomplete .An "extends" relationship indicates
alternative options under a certain use case.

23
Base use .....................................<<include>>......
Include use
case
case

Base case cannot operate without include use case.

Base use ...................................<<extend>>................. Extend use


case
case

Base case can operate without extend use case.

Example: A use case model for TV remote system.

TV Remote Control

Signal setting
Increment/decrem
<<include>> Mute
ent channel

Push
Button <<include>
>

<<include>>
<<include>>
On/off

menu

Increase/decrea <<include>>
se volume
<<include>>

Brightness Color setting


setting

24
Example: A use case diagram for a ATM system.

Activity Diagram:-
An activity represents an operation on some class in the system that results in a change in the state
of the system. An activity diagram illustrates the dynamic nature of a system by modelling the flow
of control from activity to activity.

An activity diagram is essentially a flow chart, showing flow of control from activity to activity.

Basic Activity Diagram Symbols and Notations:


Action states: Action states represent the non interruptible actions of objects. You can draw
an action state in Smart Draw using a rectangle with rounded corners.

Action Flow: Action flow arrows illustrate the relationships among action states.

25
Initial State: A filled circle followed by an arrow represents the initial action state.

Final State: An arrow pointing to a filled circle nested inside another circle represents the
final action state.

Branching: A diamond represents a decision with alternate paths. The outgoing alternates
should be labelled with a condition or guard expression. You can also label one of the paths
"else."

Synchronization: A synchronization bar helps illustrate parallel transitions. Synchronization


is also called forking and joining.

fork

Join

Example: Activity diagram for login.

26
27
Draw collaboration, sequence, use case diagrams for a hospital management system.
1. Takes appointment
A: Patient 10. Pay fees B: Receptionist

4. Confirm appointment
9. Ask for Payment
5. Consult doctor 7. Treat 2. Takes appointment
Patient from doctor
8. Patient OK
3. Doctor available

D: Nurse
C: Doctor

6. Assist Doctor

Collaboration diagram for hospital management system.

P1:Patient R1:Receptionist D1:Doctor N1:Nurse

2.
1.

4. 3.
5. 6.

7.

8.

9.

10.

Operations name are same as in above collaboration diagram.


Sequence diagram for hospital management system

28
State Machine/State Chart: A state machine is anything that can have different
states. In many cases when we are talking about states, we are talking about the
different states of an object. This diagram models the dynamic flow of control from
state to state within a system.

Sates:
States represent situations during the life of an object. You can easily illustrate a state in
Smart Draw by using a rectangle with rounded corners.

Transition
A solid arrow represents the path between different states of an object. Label the transition
with the event that triggered it and the action that results from it.

29
Initial State
A filled circle followed by an arrow represents the object's initial state.

Final State
An arrow pointing to a filled circle nested inside another circle represents the object's final
state.

Synchronization and Splitting of Control


A short heavy bar with two transitions entering it represents a synchronization of control. A
short heavy bar with two transitions leaving it represents a splitting of control that creates
multiple states.

Example of a State Chart:

30
Process and Threads:

In computer science, a thread of execution is the smallest sequence of programmed


instructions that can be managed independently by an operating system scheduler. A thread is
a light-weight process. The implementation of threads and processes differs from one
operating system to another, but in most cases, a thread is contained inside a process.
Multiple threads can exist within the same process and share resources such as memory,
while different processes do not share these resources. In particular, the threads of a process
share the latter are instructions (its code) and its context (the values that its variables
reference at any given moment). A process can be composed of several threads executing
in parallel.

A process with many threads of execution on a single processor.

Event and Signals:


In computing, an event is an action or occurrence detected by the program that may be
handled by the program.

There are two types of events.

31
1. Internal events. These are the events that are passed among the objects are resides
inside the system.
2. External events: These are the events that are passed between system and it’s
user.

Signal:
A signal is a kind of event that represents some message specification which is
communicated between objects.
Example:

Package Diagrams:
Package diagrams organize the elements of a system into related groups to minimize
dependencies among them.

Basic Package Diagram Symbols and Notations:


Packages
Use a tabbed folder to illustrate packages. Write the name of the package on the tab or inside
the folder. Similar to classes, you can also list the attributes of a package.

32
Visibility
Visibility markers signify who can access the information contained within a package.
Private visibility means that the attribute or the operation is not accessible to anything outside
the package. Public visibility allows an attribute or an operation to be viewed by other
packages. Protected visibility makes an attribute or operation visible to packages that inherit
it only.

Dependency
Dependency defines a relationship in which changes to one package will affect another
package. Importing is a type of dependency that grants one package access to the contents of
another package.

Example:

33
Time Diagram:

Time diagram are used to explore the behaviours of objects throughout a given period of
time.

Basic Terms and Concepts:


Lifeline: Lifelines indicate the object's presence over time.

Lifeline

Explain sequence diagrams.

State: Time diagram shows many states of object that might change over the time.

States

Destruction Event: This is represented by X (cross) at the end of timeline.

Terminated

Running X

Waiting

Created

Duration Constraint: It can be represented as follows:

{20-30 sec}

34
Interaction Diagram:

Sequence diagram and communication diagram together called interaction diagrams.


Communication diagram is an interaction diagram that is based on structural organisations of
the objects that send and receive messages. Class diagrams, Collaboration diagrams,
Sequence, diagrams etc. are interaction diagrams.

Architectural Modelling:

Architectural modelling in UML is for modelling the physical aspects of a system. In


this modelling the system and subsystem components are determined. These
components are then deployed in the target environment.

Major issues that are to be discussed in architectural modelling are:


Components, Deployments, Component diagrams, deployment diagrams.

Component Diagram:
A component diagram describes the organization of the physical components in a system.

Basic Component Diagram Symbols and Notations:


Component:
A component is a physical building block of the system. It is represented as a rectangle with
tabs.

Interface:
An interface describes a group of operations used or created by components.

Dependencies:
Draw dependencies among components using dashed arrows.

35
Example: Simple Component diagram with Interfaces: we have 3 simple components
that work together to create a whole system.

Deployment Diagrams:
Deployment diagrams depict the physical resources in a system including nodes,
components, and connections.
Basic Term and Concepts:
Component: A node is a physical resource that executes code components.

Association: Association refers to a physical connection between nodes, such as Ethernet.

Components and Nodes: Place components inside the node that deploys them.

36
Example of a deployment diagram:

Difference between node and component:


1. Nodes execute the components. Components participate in execution of a system.
2. The components are deployed within the nodes physically. Components are
physically packaging of the logical elements such as classes, interfaces, and
collaborations.

Difference between components and classes:


1. Components are physical aspect of a system but classes are logical aspects of a
system.
2. Components reside within the nodes but classes do not reside within the node.
3. Component may implement classes and methods. But do not have the attributes and
operations on their own. Classes have attributes and operations.

37
UML Diagram Types that we have studied:

38
39

You might also like