Demystify SOLID: I For "Interface Segregation Principal"

Welcome to the Demystify SOLID article series. In this series we are talking about the SOLID principals of software development. In our previous example we have seen three principles of SOLID. I hope you have gone through them and understood. Here the links for your reference:

This article explains the interface segregation principal. So, what does it mean. Let's try to understand with an example. I believe that without an example it would be like eating bread without jam.

So, let's take an example of one small employee class as in our previous example. Have a look at the following code.

public interface IEnployee

{

    void AddEmployee();

}

public class employee :IEnployee

{

    public void AddEmployee()

    {

        Console.WriteLine("Employee added in DB");

    }

}

We have created an IEmployee interface and implemented it in the employee class. Very simple and within the AddEmployee() function we implemented the logic to add new employee information to the DB. Now, after certain days, the requirement has arrived to read the employee information too, along with saving employee functionality.

Now, being a crazy and novice developer (like me) you might add another method, probably readEmployee() in this interface and implement the method in the employee class.

It will work fine, but have you noticed that you have now broken one principal? We are trying to modify the class, yet the “open close principal” says not to modify the class but to extend the class.

Anyway, let's understand the key point of “Interface segregation principal”. When new functionality is to be added, it's always good to implement new functionality in the new interface. Have a look at the following implementation.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net.Http;

using System.Net.Http.Headers;

using System.Text;

using System.Threading.Tasks;

 

namespace EntityFramework

{

    public interface IEnployee

    {

        void AddEmployee();

    }

    public class employee :IEnployee

    {

        public void AddEmployee()

        {

            Console.WriteLine("Employee added in DB");

        }

    }

    public class IEmployeeV2 : IEnployee

    {

        public void AddEmployee()

        {

            Console.WriteLine("Employee added in DB");

        }

        public void ReadEmployee()

        {

            Console.WriteLine("Read Employee Data");

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            IEmployeeV2 Obj = new IEmployeeV2();

            Obj.AddEmployee();

            Obj.ReadEmployee();

            Console.ReadKey();

        }

    }

}

Here we have created one brand new interface that has implemented the old interface and we have implemented the interface in a new class of the employee operation. Which will fulfill the new user's requirements.

Here is sample output.



Conclusion

In this article we have understood the interface segregation principal. I hope it will make you a better developer. In the next article we will talk about the last principal of SOLID.

Up Next
    Ebook Download
    View all
    Learn
    View all