How Interface helps in Loose Coupling – C# Example

The definition of the Interface may vary depending on the understanding and experience of the developer.Most of the time, It is one of the confusing topic among programmers.In this post, I will try to explain how interface helps in loose coupling of the software components.

Related: Learn more about C# basics on  C# for Beginners Series

What is Interface?

In C#, Interface is the contract between the interface itself and the class that implements it. The contract is simple, any class that implements the interface must provide the implementation of its properties, methods or events.

Let’s create an Interface named IAreaCalculator.

It is the good practice to start the naming of interface with the word ‘I’, So it makes clear that it is an interface.

Now, I will create a Class and implement the above-created interface for that class.

The class name is RectangleArea.

As soon as the class RectangleArea implements the IAreaCalculator. It must provide the implementation for CalculateArea function, as it is declared in IAreaCalculator Interface.

This is the reason that, developers called an interface a contract between depending parties.

Why should we need Interfaces?

While working on the large project, Interfaces are very beneficial to design our code structure in such a way that we can get a plug and play like structure.

Interfaces help in the component-based programming, it means- the code structure will be loosely coupled with each other components which leads to easier maintainability and more scalability.

If we need to change the software codes for any purpose in later future, the loosely coupled software components facilitate us to change the minimal or even less code to achieve the goal.

The benefit of using Interface.

Before deep diving into the example, it is important to understand the concept of loosely coupling and tightly coupling.

Loosely Coupling: In this case, the software components slightly depend on each other. If we need to change at one component then it will not affect much more on the dependent component. We can achieve this situation through interfaces. The classes can communicate with each other with the help of an interface.

Tightly Coupling: This situation is the opposite of loosely coupling where components are highly dependent on each other. The software components were dependent on each other in such a way that, for a small change in one component, you might need to change a lot in the dependent component. This is not the good practice in software development.

The situation of tight coupling.

Here, the classes RectangleArea and SquareArea have a function CalculateArea which is supposed to return their respective areas. The static values of length and width can be replaced by any other dynamic values in the real application.

However, I have hardcoded these values for the sake of simplicity. Later in the MainClass, I have created the instance of SquareArea and RectangleArea to access the function CalculateArea.

Here the GetArea function on MainClass is highly dependent on RectangleArea and SquareArea. Whenever you add another class to calculate the area, you have to repeat the same process and your MainClass will be even more dependent.

Our goal is to make the MainClass literally unaware about the RectangleArea and SquareArea classes. This will helps in the loose coupling between the software components.

So, let’s try to achieve the same thing using interface for the purpose of the loosely coupling component.

The situation of loose coupling.

Here I have created an interface IAreaCalculator by defining an only one function CalculateArea. The classes RectangleArea and SquareArea are implementing that interface and the area is calculated. The pieces of stuff are pretty simple. right?

Now let me get the areas from these classes.

Did you notice the GetArea function? The code block is pretty same as we have done in tight coupling example. The only difference is that the left side has changed but, still the MainClass is highly dependent on RectangleClass and SquareClass.

Let me create a middle layer which will be used for supplying the required object for our MainClass.

Here, the MainClass is totally unaware about the RectangleClass and SquareClass. The AreaFactory plays the role of abstraction to fulfill the responsibility of providing the required object to our MainClass.

If you have any queries or even better understanding about how interface helps in loose coupling, let’s discuss on the comment section below.

About dipneupane

Dip is a passionate Software Developer based in Kathmandu, Nepal. He chew code and smash keyboard for living. Apart from that, he writes about almost everything on buzzedcode.com.

View all posts by dipneupane →

One Comment on “How Interface helps in Loose Coupling – C# Example”

Leave a Reply

Your email address will not be published. Required fields are marked *