Reasons behind Interfaces popularity

There is not one but many advantages of using interfaces , one of the advantage of using interfaces in a team of 100 developers is that the architect can take sole responsibility of designing interfaces based architecture thus assuring themselves that their team will be aligned with the functional specification.

One can surely say that throught interface we can

  • Maintain the Standardization or uniformity in the method definition
  • Implement Decoupling.

Maintaining Standardization in our methods

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

As we can see Dbwork and Dbtask are two methods created by two developers , Both have used different naming conventions however if we have derived these both classes through an interface the mistake could have been avoided.

Implement Decoupling

We can use interfaces inorder to make our code maintainable and flixible. Below I am going to demonstrate an example of code decoupling through interfaces.

namespace InterfaceExample
{
interface IOperations
{
void IGetData();
}


public class ExporttoPdf : IOperations
{
public void IGetData
{
//Code to Read data and export to Pdf
}
}
public class ExporttoExcel : IOperations
{
public void IGetData
{
//Code to Read data and export to Excel
}
}
public class Client
{
public void Export(IOperations InterfaceObj)
{
InterfaceObj.IExportData();
}
}
}

In the above code i have applied decoupling in a way that a defined interface IOperations has been implemented in two classes both the classes have their own implementation of the method. Lastly i have used a client class that calls the method IExportData based upon the reference InterfaceObj holds.

One of the more challenging parts of C# programming is knowing when to create an interface and when to use an abstract class in cases in which you want to describe functionality but not implementation. The general rule is this: When you can fully describe the concept in terms of “what it does” without needing to specify any “how it does it,” then you should use an interface. If you need to include some implementation details, then you will need to represent your concept in an abstract class.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s