• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Coupling and cohesion

 
Ranch Hand
Posts: 69
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Likes 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the detailed explanation Jason!
 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic