aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Cohesion :SCJP 5.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Cohesion :SCJP 5.0" Watch "Cohesion :SCJP 5.0" New topic
Author

Cohesion :SCJP 5.0

Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
hi

I saw this at a website. Can someone explain how to figure out if cohesion is low or high ?

Which of the following have high cohesion?
Which have low cohesion?

A car

A sealed box containing all the parts of a car

A method with an integer argument N, which

contains error messages and

prints out error #N when N is passed to it

A method with nested loops

A method with loops; one runs, then the other runs

A class in which every method contains

a call to at least one other method in the same class

A class in which

no method calls any other method in that class


Review Q: should one design for high or low cohesion?

Thanks
Devi
[ April 30, 2005: Message edited by: Sreedevi Vinod ]
Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
hi

Can somebody help me with the above question ?
Any links to useful resources also would be really helpful.

Thanks a lot
Devi
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

a high cohesion is what you want. it means your class is more specific rather than general.

for instance, a class that display stuff on the screen should not store the data, because its less cohesion. to get more cohesion, you aplly the pattern MVC, where you define a class that deals with displaying the data, another that deals with storing the data and another that act as a communication layer. its kinda hard to say everythig in 10 words, read on the "design pattern".

Visitor, Mememto, Observer, MVC, Command... cool patterns, cool stuff. Paterns ensure you get high cohesion and low coupling.

Alex
[ April 30, 2005: Message edited by: Alex Turcot ]
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
A sealed box containing all the parts of a car ...
This is most probably a low cohesion.

If you had a box containing only parts related to the brakes, it would be of higher cohesion.

In this case, your class doesnt do 3404893 things. Its more narrowed.

Because of this, its much more simpler to debug it, to understand it - when you code it, and maintain it.

The 2 things you have to balance when designing is the cohesion and the coupling.
Your goal is to have a high cohesion and a low Coupling. The coupling is the dependency between your classes.

if you push it, your system can have only 1 class, it would be a very low Couplin but a very low cohesion.

You need find the best fit between the 2 concepts. You dont want to much dependency between your classes (if you modify the code at one place, you want the rest of the code to still work) and you want the cohesion to be high (you dont want to get to the point where you define the class DoAll: If I recall this is refer to as the "God class".).

type in "cohesion and coupling" in google, youll find some information.
like this one.. Cohesion and Coupling

Oh, a pattern I forgot to mention in the previous post is KISS: "Keep It Simple Stupid".

good luck,
Alex
[ April 30, 2005: Message edited by: Alex Turcot ]
Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
Thanks Alex. I've got a basic idea of these concepts. Also I managed to read some documents on cohesion and coupling on the web.
What kind of questions can one expect from this topic ?
Can any of the SCJP 5.0 beta takers share their experience ?
Please don't post any real exam questions, but if anybody can give some idea about what to expect, it will be a great help.

Thanks
Devi
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Do they really talk about cohesion and coupling in the 5.0 beta ??

To me, this is more related to developer certification and not really to programmer.

Alex
Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
Alex, you can see the objectives for the SCJP 5.0 exam here

The relevant subobjective is "Develop code that implements tight encapsulation, loose coupling, and high cohesion in classes, and describe the benefits."

Can anyone give me an idea of the kind of questions I can expect from this ?
Also I have posted some questions in my first post(above) in this thread. Can someone help me with these ?

Thanks
Devi
Mikalai Zaikin
Ranch Hand

Joined: Jun 04, 2002
Posts: 3178
    
    3
Howdy !

Coupling could be demonstrated by the following example (the code is for demonstration purpose only )



Tight coupling (bad) :


Loose coupling (better) :


The idea is: reduce "knowledge" one class about other classes. Reduce dependency of one class on another class' structure.

HTH,
MZ


Free SCDJWS 5.0 Study Guide - SCDJWS 5.0 Quiz (How to get SCDJWS 5.0 Quiz)
Java Platform, Enterprise Edition 6 Web Services Developer Certified Expert Exam Study Guide and Quiz
Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
Thanks Mikalai.
Can you give some examples for cohesion also ?
I'm waiting for you to complete this topic in your SCJP tiger study guide.

Thanks
Devi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cohesion :SCJP 5.0
 
Similar Threads
About cohesion in Java
drag and drop question
Encapsulation, coupling and cohesion
High cohesion
Roles as Concepts