• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Coupling!!

 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,


I have read the topic of coupling from 2nd chapter of K&B
but i am unable to understand the main concept of it,
what is loose coupling and tight coupling ?
please if anyone could
ellaborate and explain me (if possible with a example) it will be kinda of you

Thanks in advance.......
 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Coupling in general is how much one class knows about another class, in other words, if you have two classes A and B, and let's say that class A has public members, then class B knows much about class A, which means that class B can access those member directly, and this means that the realtion is highly coupled.

The solution to build low coupled components, is to implement encapsulated classes, which means you make the members of your class private, providing public accessors and mutaors for those members.

I hope that everything is clear now ...

Best of luck ...
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Vassili Vladimir
atleast now i understood the concept................






Preparing SCJP 1.5
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book says coupling is subjective concept and they will not drill you down too much on to this. From one perspective, coupling & cohesion reside above the concepts of inheritance and encapsulation.

Before i go on have two things in mind,
1. Reference and objects are not same.
2. Parent references can refer child objects.

Coupling as the term suggests is the measure of how two classes are "coupled", the measure of how two classes communicate. Ever tried database programming? The "Driver" that you use for connecting to database is nothing but an interface.

You dont know how a database-driver is written, all you know is if you have a driver you can use it with a dot operator [ . ] to call connect function which will give you Connection object, and this Connection object can then be used to create and execute sql queries. Have a look at the Driver interface,

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Driver.html#connect(java.lang.String,%20java.util.Properties)

Do notice the signature of the "connect" method. You dont trust anybody but the "Driver" interface, anything that IS-A driver makes sense to you. Your class/app relies upon "Driver" and you trust on all database-drivers that implement this interface and thereby will and must have the connect method implemented.
Coming to the other side, the vendors, they say, "we have a good database service and a driver that you can trust because our driver implements Driver interface"

in terms of code this may roughly look like...


This may not the actual implementation of the drivers but on a rough basis things will be coupled this way. Your class/app is least bothered about on all those internal implementations of how to connect to database. Further you can switch between two databases or better put "plug" another database using respective "Driver interface". It means you can connect to oracle using oracle driver, and MySQL using MySQL driver, however the implementations from your perspective will be the same, i.e. use a [ . ] operator on the Driver object, call connect method, get Connection and so on.

This is how see "loose coupling".
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !!!Akhilesh

Thanks a lot for a superb
explanation




Preparing SCJP 1.5
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dhwani mathur:
Hi !!!Akhilesh

Thanks a lot for a superb
explanation




Preparing SCJP 1.5



My pleasure, happy preparation.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic