File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Coupling and cohesion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Coupling and cohesion" Watch "Coupling and cohesion" New topic
Author

Coupling and cohesion

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
can anyone help me out in the concept of coupling and cohesion.the concept given in SCJP book is really confusing..i also surfed but i didnt get any useful post.can anyone help me with a neat explanation about coupling and cohesion with example.thanks in advance
Jason Bullers
Greenhorn

Joined: Dec 27, 2011
Posts: 28

What do you understand about the two concepts so far? It may be easier for us to explain and provide an example if we know how to tailor our responses. Plus, putting what you know into words may help clarify things in your own head

Do you have an idea of what the terms themselves mean? Is it just the lack of a concrete example that is giving you trouble? Are you unsure of how they relate to one another?
kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
Jason Bullers wrote:What do you understand about the two concepts so far? It may be easier for us to explain and provide an example if we know how to tailor our responses. Plus, putting what you know into words may help clarify things in your own head

Do you have an idea of what the terms themselves mean? Is it just the lack of a concrete example that is giving you trouble? Are you unsure of how they relate to one another?


yes you are absolutely correct.Lack of concrete example is the only reason which is giving me trouble.

coupling is how much your class know about other class.

cohesion is when you have one class focus, one objective.

this is what i know about it.and im unable to understand the code given in SCJP book.this the example given for cohesion which i am unable to understand.

class BudgetReport {
Options getReportingOptions() { }
void generateBudgetReport(Options o) { }
}
class ConnectToRDBMS {
DBconnection getRDBMS() { }
}
class PrintStuff {
PrintOptions getPrintOptions() { }
}
class FileSaver {
SaveOptions getFileSaveOptions() { }
}

please quote a simple example for coupling and cohesion if possible that i can grab the concept.thanks in advance
Jason Bullers
Greenhorn

Joined: Dec 27, 2011
Posts: 28

Hmm.. okay, I'll give it a try. Let's consider two classes, Zoo and Animal. Animal also has two subclasses: Tiger and Monkey. I'll define all those classes like so (and yes, this is going to be an extremely contrived example):


Have a look at the Zoo class. Do you notice anything a little bit strange about it's methods? Notice that we have more than one add method, one for each type of Animal our Zoo can have. We've coupled our Zoo to concrete types of Animal. Now what would happen if we added a new type of Animal to our Zoo? If we added a Lion, we would need a new add method for that. The Zoo knows too much about what types of Animal we have in our system. We can solve that by taking advantage of polymorphism to decouple the Zoo from all of our Animal types:


There's still a bit of a problem though. Our Zoo holds a bunch of Animals, right? But we have those other methods in there for feeding them and cleaning their cages. This is low cohesion: our Zoo class has behaviours that don't quite fit with it's primary focus. One way we can fix that is by introducing a new type of object to the system: a ZooKeeper.


Now our system has higher cohesion. The Zoo is just a collection of animals and staff, and the special behaviour of caring for the animals (feeding and cleaning) are put together in their own class. The coupling is lower as well; Zoo and ZooKeeper only care about Animal and have no ties to concrete types, which could easily change as the system evolves.

I hope that sheds some light. If not, maybe someone will think of a better example than I could!
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Thanks for the detailed explanation Jason!
kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
Jason Bullers wrote:Hmm.. okay, I'll give it a try. Let's consider two classes, Zoo and Animal. Animal also has two subclasses: Tiger and Monkey. I'll define all those classes like so (and yes, this is going to be an extremely contrived example):

swow a very neat,simple and easily understandable example.thanks a ton!!!
Jason Bullers
Greenhorn

Joined: Dec 27, 2011
Posts: 28

No problem guys. Happy to help

I just thought of another example that might be useful for you. Consider a Car that has a SteeringWheel and some Tires:


What I could do now, if I wanted to turn the car from some other class, is I could do:


If we do this, we run into the problem of high coupling again. To solve it, we could add a method to Car that allows us to turn the vehicle. This method would simply invoke turn() on its SteeringWheel instance. The coupling is going to be reduced though, because other parts of the system don't know anything about SteeringWheel (an implementation detail). If we decided to steer our car with a joystick instead, the rest of the world wouldn't care; they just tell the Car to turn() and some magic happens.

If you want to read more about this concept, search for The Law of Demeter. It's basically a set of rules that boil down to not "reaching through" an object to call methods on its internal fields.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
Another reason for not using c.getSteeringWheel().turn(30) is that the car has to turn as well as the wheel. By using car.turn(30) you are keeping to the functionality you want, viz turning the car in a particular direction. Unless you are driving (inside the car), you don’t need to know whether that turn is done with the steering wheel, or (like the “invalid carriage” which mercifully departed this country over 40 years ago) with a sort of tiller.
By keeping quiet about the wheel, you are hiding implementation details which people don’t actually need to know.
Shannon Pitts
Greenhorn

Joined: Dec 30, 2011
Posts: 13
I'd never even heard of these concepts before, but Jason's example was amazingly clear and precise. If only more tutorials could be written so well. Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Coupling and cohesion