Question

Assume methods M1 and M2 have strongly related responsibilities

First example:

If

M1 and M2 are defined within class A ( thus class A is highly cohesive )

• class B uses A.M1 and class C uses A.M2

then

A is coupled with both B and C classes

• changing the signature of M1 will require changes only in B, but not in C

Second example:

If

M1 is defined within class A1 ( thus B is coupled with A1 )

M2 is defined withing class A2 ( thus C is coupled with A2 )

then

• changing the signature of M1 will require changes only in B, but not in C

a) To my understanding, classes in last example are no more coupled than classes in first example! Or am I missing something?

b) As far as I can tell, classes in first example are more loosely coupled than those in second example only if:

  • we assume that changing the signature of M1 will also require us to change the signature of M2, but I don't see that happening very often?!

  • or if both M1 and M2 operate on the data of same type T1, then replacing T1 with T2 will require changes in both M1 and M2?!

  • or if we assume that due to M1 and M2 having closely related responsibilities, that the chances are that much greater that changing M1 will often require M2 also to be changed (even if M1 doesn't directly or indirectly call M2)?!

  • or if we assume that due to M1 and M2 having closely related responsibilities, the chances are much greater that some class will require both M1 and M2 ( as such having M1 and M2 in single class reduces coupling )?!

c) Are there any other reasons why defining M1 and M2 within A ( instead of defining M1 within A1 and M2 within A2 ) will reduce coupling?

Note - I'm aware that we should have higly cohesive modules due to easier maintenance and reusability

Thank you

Was it helpful?

Solution

Yes it does. What you are missing that is if ClassA is cohesive, changing M1 will cause ClassB to access some other method in ClassA. It increases maintenance because you won't have to modify other modules (method and variables) in ClassA itself.

When we call a class cohesive (say ClassA) , we mean that we can easily use the purpose(s) of the class without have to make our caller class (ClassB) according to the requirements of ClassA. Hence, ClassB is not dependent on ClassA and thereby cohesion is reduced.

Don't think of think of cohesion in terms of methods. Methods are not called cohesive, classes are.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top