views:

301

answers:

4

Hi,

can any one describe the exact difference between loose coupling and tight coupling in Object oriented paradigm??

I am too much confused on this.

Thanks,
Jim.

A: 

Loosely coupled clusters are a group of machines (or groups of groups) which can operate independent of each other. Communications between nodes (or subclusters) is often done via a queuing system. Tightly coupled clusters are a group of machines that are largely dependent on each other. They are often used when latency is an important factor in the application. For example, a web cluster is often a tightly coupled cluster as the web servers/application servers require quick and consistent access to a shared storage system (network filesystem or database).

giri
The question asked about object-oriented design.
Don Kirkby
+2  A: 

Tight coupling is when a group of classes are highly dependent on one another.

This scenario arises when a class assumes too many responsibilities, or when one concern is spread over many classes rather than having its own class.

Loose coupling is achieved by means of a design that promotes single-responsibility and separation of concerns.

A loosely-coupled class can be consumed and tested independently of other (concrete) classes.

Interfaces are a powerful tool to use for decoupling. Classes can communicate through interfaces rather than other concrete classes, and any class can be on the other end of that communication simply by implementing the interface.

Example of tight coupling:

class CustomerRepository
{
    private readonly Database database;

    public CustomerRepository(Database database)
    {
        this.database = database;
    }

    public void Add(string CustomerName)
    {
        database.AddRow("Customer", CustomerName);
    }
}

class Database
{
    public void AddRow(string Table, string Value)
    {
    }
}

Example of loose coupling:

class CustomerRepository
{
    private readonly IDatabase database;

    public CustomerRepository(IDatabase database)
    {
        this.database = database;
    }

    public void Add(string CustomerName)
    {
        database.AddRow("Customer", CustomerName);
    }
}

interface IDatabase
{
    void AddRow(string Table, string Value);
}

class Database : IDatabase
{
    public void AddRow(string Table, string Value)
    {
    }
}

Another example here.

jonathanconway
So far what you said makes perfect sense.. Can you please explain how coupling mechanism can relate to Observer pattern?
Jim
The observer pattern is described here: http://en.wikipedia.org/wiki/Observer_pattern. Since the Subject class can maintain a list of classes that inherit from 'Observer', without actually knowing the concrete type of those classes, this is an instance of loose coupling. The Subject doesn't depend on any of its Observers or their internal concerns. The observers don't depend on the Subject or any of its concerns.
jonathanconway
+3  A: 

In object oriented design, the amount of coupling refers to how much the design of one class depends on the design of another class. In other words, how often do changes in class A force related changes in class B? Tight coupling means the two classes often change together, loose coupling means they are mostly independent. In general, loose coupling is recommended because it's easier to test and maintain.

You may find this paper by Martin Fowler (PDF) helpful.

Don Kirkby
yes you are right. Making a perfect sense. Thanks.
Jim
+1 for the link
Marcel
A: 

Loose coupling is and answer to to old style hardcoded dependencies and related issues issues like frequent recompilation when anything changes and code reuse. It stresses on implementing the worker logic in components and avoiding solution specific wire up code there.

Loose Coupling = IoC See this for easier explanation.

MSIL
I don't think loose coupling is the same as inversion of control. Inversion of control is a very useful technique for reducing the coupling of your design, but there are many other techniques.
Don Kirkby