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
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.
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,
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.