Implement decouple architecture in Interface

Sourav.Kayal
Posted by in C# category on for Intermediate level | Points: 250 | Views : 20651 red flag

This article will explain how to implement decoupling architecture using c#

Develop decouple architecture using interface in C#


In general, the term coupling represents the relationship between two objects. Couplings are two types

1)      Tight coupling

2)      Loose coupling

Tight coupling:-

Tight coupling represent very close relationship between two entities.  And most of the time one entity is very closely dependent on another entity. And if you change one entity then it’s effect on another one.  Let me give one small example in programming perspective. Let’s Think, there is three layer architecture in project.  Where each layer is very heavily related with other one. Now if you change one layer say User interface layer then you might have to change business layer also ,because your layer are tightly couple with each other. And always we will try to avoid this pattern

Loose coupling:-

Loose couple represents very slight dependency between entities.  And if we represent three layer architecture as a loosely couple then change of one layer will not much effect to another one. Or with very little amount of change we can get full functionality. And in software development always we should build de couple architecture.

Now question is how we can achieve de-coupling ?  

There are no shortcuts for that or there is no single line answer. It will come from your experience.  If you practice few guideline and good practice then you can achieve this. Now speak technically .

Use interface to implement de couple architecture.

Yes , using interface you can reduce coupling between classes. When two classes want to communicate with each other just use interface between them and through interface, let them communicate. In below example I am giving one example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
 
namespace BlogProject
{
    interface IPerson
    {
        void ShowCountry();
    }
 
    class IndianPerson:IPerson
    {
        public void ShowCountry()
        {
            Console.WriteLine("I am Indian Person");
        }
    }
    class USPerson :IPerson
    {
        public void ShowCountry()
        {
            Console.WriteLine("I am US Person");
        }
    }
 
 
    class PersonSupplyer
    {
        public static IPerson ReturnPerson(String country)
        {
            if (country == "INDIA")
                return new IndianPerson();
            else if (country == "US")
                return new USPerson();
            else
                return null;
        }
 
    }
 
   class Program
    {
        static void Main(string[] args)
         {
             IPerson objPerson = PersonSupplyer.ReturnPerson("INDIA");
             objPerson.ShowCountry();
             Console.ReadLine();
         }
    }
}
 

Here I have created one Interface like

    interface IPerson
    {
        void ShowCountry();
    }

This interface contains one function .And now I am implementing this interface in two classes in my middle layer or business layer. Like below

class IndianPerson:IPerson
    {
        public void ShowCountry()
        {
            Console.WriteLine("I am Indian Person");
        }
    }
    class USPerson :IPerson
    {
        public void ShowCountry()
        {
            Console.WriteLine("I am US Person");
        }
    }

 And one more class I have kept in middle layer to supply proper object of concrete class.

class PersonSupplyer
    {
        public static IPerson ReturnPerson(String country)
        {
            if (country == "INDIA")
                return new IndianPerson();
            else if (country == "US")
                return new USPerson();
            else
                return null;
        }
    }

The purpose of this class is to generate appropriate object of concrete class. Now my client is here

static void Main(string[] args)
         {
             IPerson objPerson = PersonSupplyer.ReturnPerson("INDIA");
             objPerson.ShowCountry();
             Console.ReadLine();
         }

And my client is totally unaware about my concrete class. It only knows which parameter need to send to get which class’s object .

Now, what is Advantage of that?

The main advantage of this design is client is totally unaware about concrete class representation. Because,  ersonsupplyer class is making abstraction over my concrete class.

The second advantage is , if we add one more person class in my middle layer then no need to change any code in client layer. Just it’s enough to inform client developer which parameter need to supply to get object of newly added class.

The third advantage is we can control new keyword in our client code. As only in one place we are creating new keyword.


Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Posted by: Vishvvas on: 6/25/2013 | Points: 25
This is good illustration. There are some notes as following
1. Looks like whatever you have described is "Decoupled design" and not the "decoupled architecture"
2. By theory "Coupling is measure of how interconnected the code modules (classes, subsystems)"
3. Low coupling is one of the GRASP patterns.
4. Coupling always go hand in hand with cohesion.

Hope this helps.

Login to post response

Comment using Facebook(Author doesn't get notification)