This Article explains the Purpose and use of interfaces. We can learn when and where we need to use interfaces and how it helps us in Object oriented programming.

Introduction

Interface is basically a class which will have empty method(s), delegate(s) or event(s). The implementation of the methods is done in the class that implements the interface, that is the reason the interface's method will not have definition.

Interface will be defined using the keyword interface. Interfaces would be used as a intermediater in most of the software program.

Purpose


Interface is used for the below purposes:
  1. To maintain the Standardization or uniformity in the method definition
  2. It enables to implement Decoupling.

Standardization

It is very much possible that multiple developers write code for common operations and they will use their own naming convention for their methods. In order to maintain the uniformity in the code, we can use interfaces. The Senior developer or architect can design the interfaces with common method names, that can be used by mulitple developers. So the developers cann't use their naming conventions for the method. It brings the more readablity too.

Decoupling

Decoupling is very important in software architecture design. The system is said to be good system, when one process doesn't impact the other.So, The interface helps us to seperate one process from impacting other and the future implementation should not affect the currect process. Basically our system should be expandable with out impact.

Let us look at the first purpose : Standardaization

namespace InterfaceExample
{
    interface IDboperation 
    {
        public void insertData();
    }
    public class Dbwork
    {
        public void updatetoDb();
    }

    public class Dbtask
    {
        public void insertdb()
        {

        }
    }
}

In the Above example, there are two classes written by two different developers [Class Dbwork and Class Dbtask which as two different function. One developer named it as updatetoDb and another developer named it as insertdb. Instead, if we insist developers to implement the interface, they will use the common function called insertdata(); That way we bring uniformity and standard in our development.

Purpose of Decoupling:


namespace InterfaceExample
{
    interface ICommoanOperations
    {
        void IExportData();
    }
    public class ExporttoPdf :ICommoanOperations
    {
        public void ExportDataToPdf
        {
        //Code to Read data and export to Pdf
        }

    }
    public class ExporttoExcel :ICommoanOperations
    {
        public void ExportDatatoExcel
        {
         //Code to Read data and export to Excel

        }
    }
    public class Client
    {
        public void Export(ICommoanOperations InterfaceObj)
        {
            InterfaceObj.IExportData();
        }

    }
   
}
In the above example, there are two classes , one for exporting data to pdf and other for excel. Now the interface acts as a intermediater between two classes. Instead of calling the direct concrete class methods, we can call interface that calls the respective class method. This enables decoupling in our program. More over, if tomorrow , we need another fuctionality , we can attach easily.

Conclusion

It is good practice to develop code using Interfaces. And that is the reason, we implement WCF Services using Interfaces.