Coupling Cohesion
Coupling Cohesion
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
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
High Coupling
Content Avoid
Common
External
Control
Loose
Stamp
8
Cohesion
• High cohesion facilitates reuse (well defined modules)
High Cohesion
Functional
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental Low
10
Examples of Cohesion
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
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
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
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
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