Couplingandcohesion
Couplingandcohesion
It is also on of the feature of OOPS. The degree of dependency between the objects is
called Coupling.
Example !
class "
#
static int i $ %.m&'()
.
.
.
.
*
class %
#
static int m&'(
#
return c.j)
*
*
class C
#
static int j $ +.l)
.
.
.
.
*
class +
#
static int l $ ,-)
.
.
.
.
*
These components are tightly coupled with each other. .ith out effecting any component we
can/t modify
any component. This type of programming is not recomanded.
.hen e0er we are de0eloping the components compulsory we should maintain 0ery less
dependency
between the components i.e we ha0e to maintain loosely coupling.
The main advantages of loosely coupling are:
1( It impro0es maintainability.
&( It ma2es enhancements easy.
,( It produces reusability.
Cohesion :-
3or e0ery component we ha0e to define a well defined single purpose. Such type of
components are said to be followed high 4cohesion5.
Ex:-
If we define a single ser0let for all re6uired functionalities of mail application li2e displaying
login page7 8alidating the user7 +isplaying Inbox page etc9
Then for e0ery small change entire component will be disturbed and it reduces maintainability
and
suppresses reusability of code.
To resol0e this problem. 3or e0ery tas2 we can define a separate component li2e login.jsp for
displaying
login page7 0alidate ser0let for 0alidation purpose7 inbox ser0let7 3or displaying mails etc9.
%y using this approach we can perform the modification in any component with out effecting
the remaining.
"nd it impor0es maintainability. It also promotes reusability. .hen e0er 0alidation re6uired the
same
0alidate ser0let we can use.
The main advantages of High Cohesion are:
1( It impor0es maintainability.
&( Enhancements is easy.
,( Promotes reusability.