Money Mgt Calculator
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Beginning Java
 
RSS feed
 
New topic
Author

coupling and cohesion in java

jeya prabha
Greenhorn

Joined: Jun 03, 2008
Messages: 23

what is coupling and cohesion in java1.5
what is loose coupling,tight coupling?
please give me some sites for good materials for this.
Jesper Young
Java Cowboy
Bartender

Joined: Aug 16, 2005
Messages: 7843

jeyaprabha, you have been warned to change your display name to make it conform to the JavaRanch naming policy multiple times, and yet you have not changed it.

Please read the naming policy carefully and change your display name. Specifically, your display name must consist of a first and a second name. Not just a first name.

If you do not change your display name immediately, your account will be locked.

Java Beginners FAQ - JavaRanch SCJP FAQ
The Java Tutorial - Java SE 6.0 API documentation
jeya prabha
Greenhorn

Joined: Jun 03, 2008
Messages: 23

i have changed my display name
Campbell Ritchie
Bartender

Joined: Oct 13, 2005
Messages: 14934

Originally posted by jeya prabha:
i have changed my display name
Thank you.

Now you can have an answer.

Cohesion means that the whole of a class sticks together (well, roughly). A class should be responsible for itself, should do one thing and as far as possible do everything for that one thing. A Car class should remember its make, colour, speed. It is responsible for changing speed; the speedUp() and slowDown() methods should be in the Car class; no other class should make your Car go faster or slower.

Cohesion is (to quote Sellars and Yeatman) A Good Thing.

Coupling means that one class gets at the implementation of another class.The Car class has allowed access to its speed field and the Driver class changes its value directly. This means other classes gain access to the implementation of the Car class; any changes to that implementation will "break" the Driver class. This is "tight coupling" and tight coupling is A Bad Thing, because any changes to one class can mean that other classes would have to be altered too.
To avoid tight coupling
  • All classes should have as small a public interface as possible.
  • All non-constant fields should have private access.
  • Any alterations to the values of fields should be via method calls.
  • CR
    camilo lopes
    Ranch Hand

    Joined: Aug 08, 2007
    Messages: 164

    simple,

    coupling = is the how much your class know about other class. let say that you have one friend and you know a lot about this friend as: What she do in the bathroom?
    but this you shoudn't know.
    The same happen with the class, when one know a lot about other.

    cohesion = is when you have one class focus, one objective. for exaple:

    this is one class com high cohesion. because she has one focus. one class with low cohesion is when she does several somethinds example:
    - add customers
    - print list of the customers
    - connect BD
    all in one unique class this is one class com low cohesion.


    Brazil - Sun Certified Programmer Java 1.5
    http://www.camilolopes.com/ About Java - Update every Week.
    twitter: http://twitter.com/camilolope
     
     
     
    Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Beginning Java
     
    RSS feed
     
    New topic
    Money Mgt Calculator

    .