1

How would you define the following code?

a) High cohesion

b) Low cohesion

I would say High as even though takeAndGlue() does 2 things they are called with 2 separate methods, hence the stackTrace is traceable.

public class Assembler()
{
    public void take()
    {
        System.out.println("Take the thing");
    }
    public void glue()
    {
        System.out.println("Glueing the thing");
    }
    public void takeAndGlue()
    {
        take();
        glue();
    }
}
Rollerball
  • 12,618
  • 23
  • 92
  • 161
  • This question is impossible to answer for such a trivial and out-of-context example. – chrylis -cautiouslyoptimistic- Aug 30 '13 at 07:59
  • @chrylis thanks for your feedback. However I am preparing for the OCPJP7 and these questions are part of the exam topics (present in mock exams) – Rollerball Aug 30 '13 at 08:00
  • *beats head on desk* This example still makes no sense for the question. *Maybe* if `takeAndGlue` took a `Thing` as a parameter... I can see an argument either way, but on a forced-choice test, I'd probably go with high because you can make a good case for sequential cohesion in processing `Thing`s (even though they're not processed in this API...). – chrylis -cautiouslyoptimistic- Aug 30 '13 at 08:03
  • 1
    @chrylis why does it not make sense? I mean it's clearly forced. However I thought it was "high" the answer. However in the mock is low. I think they are wrong. Thanks for responding – Rollerball Aug 30 '13 at 08:05
  • What is the relation of the question to design patterns? Regarding your inquiry, I think the class has high cohesion, since the methods perform only one task or kind of task: printing. – arjacsoh Aug 30 '13 at 08:31
  • @arjacsoh you right I put it there by mistake sorry. Corrected. – Rollerball Aug 30 '13 at 08:42

3 Answers3

1

Wikipedia says..

As applied to object-oriented programming, if the methods that serve the given class tend to be similar in many aspects, then the class is said to have high cohesion.

In your example, all the three methods are doing work related to assembly, and this class can be said to have high cohesion.

Amol Sonawane
  • 1,130
  • 7
  • 10
0

This class shows low cohesion because take() and glue() can be called separately but that makes no sense if not in the right order. In other words: take(), glue() should not be public.

KocsisLaci
  • 59
  • 3
0

This is an example of low cohesion:

class Cal
{


     public static void main(String args[])
     {

          //calculating sum here
          result = a + b;
          //calculating difference here
          result = a - b;
          //same for multiplication and division
     }
}

this is an example of High cohesion:

class Cal
{

     public static void main(String args[])
     {

          Cal myObj = new Calculator();
          System.out.println(myObj.SumOfTwoNumbers(5,7));
      }


     public int SumOfTwoNumbers(int a, int b)
     {

          return (a+b);
     }

     //similarly for other operations

}

But high cohesion implies that the functions in the classes do what they are supposed to do(like they are named). And not some function doing the job of some other function. So, the following can be an example of high cohesion:

Dhrumil Shah
  • 736
  • 6
  • 15