0% found this document useful (0 votes)
85 views26 pages

Coupling Cohesion

Coupling refers to the interdependencies between modules, while cohesion refers to the relationships within a module. The goal is to have loosely coupled modules with high internal cohesion. There are different types of coupling (e.g. loose vs. tight) and cohesion (e.g. functional, sequential, communicational). High cohesion and loose coupling are generally desirable as they facilitate reuse, simplifiy modification, and reduce dependencies between modules.

Uploaded by

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

Coupling Cohesion

Coupling refers to the interdependencies between modules, while cohesion refers to the relationships within a module. The goal is to have loosely coupled modules with high internal cohesion. There are different types of coupling (e.g. loose vs. tight) and cohesion (e.g. functional, sequential, communicational). High cohesion and loose coupling are generally desirable as they facilitate reuse, simplifiy modification, and reduce dependencies between modules.

Uploaded by

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

Coupling / Cohesion

Coupling – concerns relationships between Modules


Cohesion – concerns relationships within a module

Goal: We want loosely coupled modules with high internal


cohesion
Coupling

• The degree of dependence such as the


amount of interactions among components

No dependencies Loosely coupled Highly coupled


some dependencies many dependencies

2
Tight coupling

Module A Module B

Module C Module D

Shared data
area
Loose coupling
Module A

A’s data

Module B Module C

B’s data C’s data

Module D

D’s data
coupling
• The more one module has to “know” about another module,
the higher is the coupling between them
• Modules should be as independent as possible
• Modules should communicate only via small, well defined
(“narrow”) interfaces
• Interfaces help separate “what” from “how”
• Proper encapsulation reduces coupling
• Change in one unit affects other units implicitly
• Ex: Adding a new command to a GUI, but forgetting to add it to
the help menu... (how could this be fixed?)
• Ex: Adding a new command to the help menu, but forgetting to
add help text for the command in the help function... (how
could this be fixed?)
Coupling Example
:
public void
printAddress (StudentRecord sr) {
// Here we need to know how to extract
// the relevant info from StudentRecord
.
// What if class StudentRecord change?
}

OR:
public void printAddress (String address) {
// This will print any address, not only students
}
Try to keep coupling low

 Common forms of coupling


– Class A has an instance of Class B
– Class A send a message to an instance of Class B
– Class A is a subclass of Class B (inheritance)
– Class A implements interface I
 Coupling does occur, in any design
 A dangerous case of high coupling:
– Allow Class A uses instance variable of Class B
– Consider low coupling important when evaluating
design
Type of Coupling

High Coupling
Content Avoid
Common

External
Control
Loose
Stamp

Data Try to achieve


Low Uncoupled

8
Cohesion
• High cohesion facilitates reuse (well defined modules)

• High cohesion simplifies modification (all relevant code in


• one place)
• High cohesion low coupling to other modules (but high
• coupling within the module..)
• Reducing coupling higher cohesion
Type of Cohesion

High Cohesion
Functional

Sequential

Communicational

Procedural

Temporal

Logical

Coincidental Low
10
Examples of Cohesion

Function A Function A Time t0


Function Function
B C logic Function A’ Time t0 + X
Function Function
D E Function A’’
Time t0 + 2X
Coincidental Logical Temporal
Parts unrelated Similar functions Related by time

Function A

Function B

Function C
Procedural
Related by order of functions
11
Examples of Cohesion (Cont.)
Function A Function A

Function B Function B

Function C Function C

Communicational Sequential
Access same data Output of one is input to another

Function A part 1

Function A part 2

Function A part 3

Functional
Sequential with complete, related functions
12
Determining Module Cohesion
Yes Doing one
Functional function
only? Coincidental

Sequential Yes No No
Module Activities
Sequence Data None Yes
related same category?
important?
by?
No Control Flow
Logical
Communicational Sequence
important? No
Yes
Procedural Temporal

13
Low Cohesive Class Example

• This class has low cohesiveness because we are dealing with three different
entities : The bus itself, the bus driver and the bus route all represented by
one single class (one class is modeling three different types objects).
• How to increase the cohesiveness of this class?? ..BY moving each one of
these Entities out into their own classes. So each class will be much more
focused
• Let us see how it looks if we move all the three entities into their won
classes.
The Bus Class

• The Bus Class which we can use to create individual buses that holds
bus number and bus fee with various methods for working with the bus
The Bus Driver Class

• The BusDriverr Class which only deals with handling the driver
of the buses in which we can set and get the driver’s name
The BusRoute Class

• The BusRouter Class which which stores the starting location (pointA and
the ending Locations PoinB) which is the route the bus will take
• Also have a method to set the route that the bus should follow
Public Transportation Class

• The PublicTransportation Class which now can use the other focus
classes which could create a bus, a bus diver and bus route objects
and call the associated methods.
• By moving the three entities into their separate own classes we created
much more cohesive classes that are focused on their own individual
task
Functional Cohesion

• All statements within a Module are base on one function


• It is the best Cohesion

Example: Calculate income tax

If Product is exempt then


incomeTax=0
Else
if productPrice > $100 Then
incomeTax =productPrice*0.5
endif
endif
Sequential Cohesion
• The instruction inside a Module are related to each other through the input
data
• Sequence of events is to be followed
• Second highest after Function Cohesion

Example

TotalPurchase =0
Read noOfPurchases
Do Loop =1 to noOfPurchases
get purchases
add purchases to TotalaPurchases
end Do
SalesTax = totalPurchases * saleTotal
End
Communicational Cohesion
• The activities are related to each others by the data that the Module uses.
• Sequence is not important in this case
• Modules will communicate with each others

Example

If transactionTYpe not = ‘0’


writeErrorReport
If customerNumber not numeric
writeErrorReport
End if
End if
Procedural Cohesion
• The instructions are grouped together because of a particular procedure order
• Sequence is not important in this case

Example

readNumberOfSTudentRecords

noOfRecords =0
totalAge=0
readStudentRecord
Do while moreRecordsExist
Add age to totalAge
Add 1 to noOfRecords
READ studentRecors
EndDo
End
Temporal Cohesion
• The instructions in the Module together through the flow of Control
• These instructions perform more than one function
• Its temporary , the initialization below is is not permanent (temporal)

Example

INITIALIZATION

Open Prompt “Enter date = MMDDYY”


READ todaysDate
Set transCount to 0
Set reportTotal to 0
Open reportFile
End
Logical Cohesion
• We use logic, The instructions are less related to each others
• Instructions are grouped tougher due to certain classes or activities
• Case is sued here
Example

Case of FileCode
Case 1: Read Case 1
if NoF
Do something
endif
Case2: Rea Case 2
If NoF
Do other thing
endif
End Case
End
Coincidental Cohesion
• Worse type
• Instructions have no relationship with each other at all

Example
File Processing

OPEN Employee Update File


READ Employee Record
Print Page Heading
Open master File (Employee)
set page-count to 1
Set error Flag to False

End
References
http://www.it.uu.se/edu/course/homepage/oop/ht02/OO_6.pdf
http://www.math-cs.gordon.edu/courses/cps122/lectures-2013/Cohesion%20
and%20Coupling.pdf

You might also like