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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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: 3099
posted
0
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.