What is loosely and tightly coupling?

In programming, we have the concept of coupling.  Coupling is the way two or more modules of our application relates to each other.

Suppose that we are building a new application.  We can divide our application in tiers. One tier will be our user interface (UI). Another ties is our business logic layer. And we could use also a data access layer.

Our UI, will send information to the our business layer.  Then, the business layer will send the information to the database layer. If the business layer determines that there is an error in the information, it will send an error message to the UI.

How we design our new 3-tier application will determine the coupling between each tiers.

Loosely or tightly coupling

ropeWe can have an application “tightly couple”. Tightly couple means that one module is so tight to another one, that when making a change to one of them, will impact the other.

For example, our business layer creates, inside itself, the data access layer.  Imagine that today we are using a data access layer that connects to an SQL Server database.  Tomorrow, the application will be moving to an Oracle database.  If this is the case, we will need to make changes to business layer to accommodate to the new database.


// Data Access Layer
public class MySqlDatabase
{  
  public decimal GetFunds(int accountId){...}
}

// Business Layer
public class MyBusinessLayer
{
  public decimal GetFunds(int accountId)  
  {
    var database = new MySqlDatabase(); // Here we are creating our SQL database layer     
    var funds = db.GetFunds(accountId);    
    return funds;  
  }
}

A “loosely couple” application means that a module is not all dependable of another. Following our previous example, the business layer use the database layer.  The business layer does not create the database layer. The business layer use the database layer by means of an “interface”.  There will be another module that will handle the creation of the database layer.

An interface to the rescue

An “interface” is a contract that all module who implement it needs to follow.  If our database layer implements an interface, we can change to another database layer who implements the same interface. One database layer implementation can use an SQL Server database. Another one can use an Oracle database.

// Database Interface
public interface IDatabase
{ 
   decimal GetFunds(int accountid);
}

// SQL Server Data Access Layer
public class MySqlDatabase: IDatabase
{ 
   public decimal GetFunds(int accountId){...}
}

// Oracle Data Access Layer
public class MyOracleDatabase : IDatabase
{
   public decimal GetFunds(int accountId){...} 
 }

// Business Layer
public class MyBusinessLayer
{   
   private readonly IDatabase _database;  

   public MyBusinessLayer(IDatabase database) 
   { 
      _database = database; // Passing the database implementation on the constructor of the class  
   }  

   public decimal GetFunds(int accountId) 
   { 
       var funds = _database.GetFunds(accountId); // Here we use the interface implementation    
       return funds; 
   }
}

How much work in the long run?

I recommend the use of loosely coupling for any application.  A loosely application appears to be a little daunting during the initial process of design and coding. But if you work in a medium to large software shop, maintainability is something to take into consideration.  Having a loosely application will help you when stake holders decide to make changes to it.

Rodnney

I am a "multi hat" software developer with more than 18 years in experience. I had worked from government agencies, insurance companies, ticketing system and educational business using a lot of technologies like SQL Server, Oracle, ASP.NET, MVC, HTML, CSS, Javascript, Linq, etc.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.