• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Coupling and Cohesion in java

 
Shahid Pathan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do we implement coupling and cohesion in java? And simple example based on real life scenario?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't really "implement" coupling and cohesion. It's a characteristic of anything you implement.

So coupling is the extent to which different classes are dependent on each other, and so the extent to which changes to one class affect others. And cohesion is the extent to which a class has a specific purpose.
 
Shahid Pathan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew Brown ,
I know that this is default characteristic but we have to take care of it when we design the classes,
so that our application should be more flexible more independent on other module.
I need a basic and simple example on real life (daily life activity) and design the classes to demonstrate coupling and cohesion.
Thanks
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want a basic example to avoid coupling and demonstrate cohesion.Now you see the Bank class has direct access to the Account class' inner workings. Not only does that mean the security and integrity of the Account class may be breached, but also any changes to the Account class will "break" the Bank class too.

That is an example of tight coupling. It is also poor cohesion, because the Account class does not work out how much money it contains. The account now contains 123.45, irrespective of what it contained before. Using deposit and withdraw methods is far better. Oddly enough, I wrote a withdraw method earlier today: here. All the calculations are done inside the withdraw method there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic