What is Tight Coupling:-
When the components
are highly dependent to each other, then if you want to change one component
then it will affect the other component. Suppose I have developed a small
application and using tight coupling then if I make any small change in the
class then I have to change all the places where I have used the object of that
class then there will be no problem but suppose we have developed a enterprise
application where single class used thousand times then if any change is required
to the class then I have to modify all those places where this class is used.
So it is difficult maintain code in this scenario. Some codes and picture are
given below to describe tight coupling in a tabular form.
What is Loose Coupling:-
Main motive of loose coupling is to reduce the inter-dependency of the
components of a system. Because it will reduce the risk on changing one
component will require
changes in any other component. So a loose coupling is nothing but a design
strategies which increase stability of entire framework, flexibility of a
system and more maintainable. Instantiation of the
actual implementation will take place somewhere else in the code. This is the
one of the most important reasons why dependency injection frameworks are being
used. Some codes and
picture are given below to describe tight coupling in a tabular form.
Real life
example of Tight Coupling and Loose Coupling are
Tight coupling
Think of your skin. It's stuck to your body. It fits like a
glove. But what if you wanted to change your skin colour from say white to
black? Can you imagine just how painful it would be to peel off your skin, dye
it, and then to paste it back on etc? Changing your skin is difficult because
it is tightly coupled to your body. You just can't make changes easily. You
would have to fundamentally redesign a human being in order to make this
possible.
God was not a good object orientated programmer.
Loose coupling
Now think of getting dressed in the morning. You don't like
blue? No problems: you can put a red shirt on instead. You can do this easily
and effortlessly because the shirt is not really connected to your body the
same way as your skin. The shirt doesn't know or care about what body it is
going on. Clothes can easily and painlessly be changed. Perfect example of
loose coupling.
Now i have describe the difference in coding standard in both
coupling :
|
|
Tight Coupling
|
Loose
Coupling
|
class CallingClass
{
private bool
isValidate;
public int SetMethod()
{
if (isValidate)
return new mainClass ().Parameter;
return 0;
}
public void Printmethod()
{
new mainClass ().Main_method ();
}
}
class mainClass
{
public int Parameter;
public void Main_method()
{
Console.WriteLine(Parameter);
}
}
|
class CallingClass
{
private ISanpleInterface intr_imp;
public CallingClass (ISanpleInterface intr_variable)
{ intr_imp= intr_variable;
}
public int SetMethod()
{ intr_imp. SetMethod();
}
public void Printmethod()
{
intr_imp..Show ();
}
}
interface ISanpleInterface
{
int SetMethod();
void Show();
}
class mainClass : ISanpleInterface
{
public int Parameter=123;
public int SetMethod()
{ return parameter;
}
public void Show ()
{
Console.WriteLine(Parameter);
}
}
|
Loose Coupling advantages
This is a small code example, but in the large
project is will be very hard to make those changes in every class.