Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

coupling and cohesion

 
sean zang
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, folks
Does anyone show me what's main difference between Coupling and
Cohesion. Simple class diagram demonstration will be greatly appreciated
many thanks
 
Laojar Chuger
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it's difficult to explain these two concepts graphically. The difference is, basically, coupling measures the connections among classes whereas cohesion measures connections inside a class.
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what "connections inside a class" means. I have seen cohesiveness defined as how related the data and functions of a class are to each other and the class itself. That is, a class which represents a single concept with data that would be reasonably related to that concept and methods that operate on that data or with each other, would be fairly cohesive.
You should strive for classes (or components, which can be a number of classes) that are cohesive and minimize coupling.
John
 
Laojar Chuger
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By connection I mean the dependence between two entities. Sorry for not clearly expressed.
 
Linda Rising
Author
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sean zang:
Hi, folks
Does anyone show me what's main difference between Coupling and
Cohesion. Simple class diagram demonstration will be greatly appreciated
many thanks

The notions of coupling and cohesion are old ones -- they were created in the 70s by Yourdon and Constantine and they referred to procedural entities in a program. The concepts still apply today because you can talk about modules and a module could be any program entity, including a class.
When a module is created the reason for its existence is called a design decision (David Parnas originated this expression -- also in the 70s). If a module has high cohesion then everything in that module is related to that design decision.
For a given class, ask yourself why you created that class and then ask if everything in the class is related to that reason. If so, then the class has high cohesion -- that's good!
If elements that relate to a given design decision are found in more than one module, then those modules are coupled. The strength of the coupling is determined by the strength of the relationship between the elements of the design decision.
If you create a class and then find that to implement the design decision that was the reason for creating the class you have to put related things in other classes -- then the classes are coupled -- sometimes this is OK but if the relationship is too strong -- this is not good.
Here's a URL with probably more than you want to know about this :-)!
http://www.iam.unibe.ch/~scg/Archive/PhD/moser-phd.pdf

Linda
Yourdon and Constantine have a list of categories to classify coupling and cohesion for procedural languages. I wrote a paper to do the same for object-based languages. I also wrote the book that you could win this week -- The Pattern Almanac!

------------------
Linda Rising
Author of The Pattern Almanac 2000
 
Rahul Mahindrakar
Ranch Hand
Posts: 1868
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out an article from javaworld here
[This message has been edited by Rahul Mahindrakar (edited February 01, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic