Wednesday, 13 August 2014

Interfcace & Loosely coupled architecture

What is interface?         
An interface is a contract between itself and any class that implements it. This contract states that any class that implements the interface will implement the interface's properties, methods and/or events. An interface contains no implementation, only the signatures of the functionality the interface provides. An interface can contain signatures of methods, properties, indexers & events

What are the advantages I am getting using interface?

·         Loosely coupled architecture. Design principle says your application should be loosely coupled
·         Component-based programming
·         Interfaces help in define a contract. Methods/ properties/ events are in interface must be in class which implement that specific interface


Why to use loosely coupled architecture

A Tightly Coupled Object is an object that needs to know quite a bit about other objects and are usually highly dependent on each other's interfaces. Changing one object in a tightly coupled application often requires changes to a number of other objects

Loose coupling is a design goal that seeks to reduce the inter-dependencies between components of a system with the goal of reducing the risk that changes in one component will require changes in any other component. Loose coupling is a much more generic concept intended to increase the flexibility of a system, make it more maintainable, and makes the entire framework more "stable


Let’s take an example

You have to define some class which connects to your sql server database. You will simply define in following way
 

 class ConnectToSqlDB
    {
        public string ConnectionString
        {
            get;
            set;
        }

        public void ExecuteQuery(string query)
        {
              // All the Sql server related code
        }
    }

You will simply define ConnectToSqlDB everywhere in your project.
Now after some time project requirement changes, now you want Oracle database to use
This creates the problem you need to rewrite the class

class ConnectToOracleDB
    {
        public string ConnectionString
        {
            get;
            set;
        }

        public void ExecuteQuery(string query)
        {

        }
    }



But disadvantage of this code is you need to change class name everywhere In your whole project.

This problem has simple solution using interface in  a following way

class Program
    {
        static void Main(string[] args)
        {
            IntefcaeConnectDatabase dbObj = DbHelper.GetInstance(TypesOfDB.Oracle);
            

        }
    }


    class DbHelper
    {
        public static IntefcaeConnectDatabase GetInstance(TypesOfDB instance)
        {
            switch(instance)
            {
                case TypesOfDB.Oracle:
                        return new ConnectToOracleDB();
                        break;
                case TypesOfDB.SqlServer:
                        return new ConnectToSqlDB();
                        break;

            }
            return null;
        }
    }


    public enum TypesOfDB
    {
        Oracle,
        SqlServer
    }


    interface IntefcaeConnectDatabase
    {
        string ConnectionString{get;set;}
        void ExecuteQuery(string query);
    }


    class ConnectToOracleDB : IntefcaeConnectDatabase
    {
        public string ConnectionString
        {
            get;
            set;
        }

        public void ExecuteQuery(string query)
        {

        }
    }


    class ConnectToSqlDB : IntefcaeConnectDatabase
    {
        public string ConnectionString
        {
            get;
            set;
        }

        public void ExecuteQuery(string query)
        {

        }

    }

No comments:

Post a Comment