Interface Segregation Principle in C#

Interface Segregation Principle in C# with a real-time Example

In this article, I am going to discuss the Interface Segregation Principle in C# with a real-time example. Please read our previous article before proceeding to this article where we discussed the Liskov Substitution Principle in C# with a real-time example. The letter I in SOLID Design Principle stands for Interface Segregation Principle which is also known as ISP. As part of this article, we are going to discuss the following pointers in detail.

  1. What is the Interface Segregation Principle in C#?
  2. Example without using the Interface Segregation Principle in C#.
  3. Example using the Interface Segregation Principle in C#.
What is the Interface Segregation Principle in C#?

The Interface Segregation Principle states that Clients should not be forced to implement any methods they don’t use. Rather than one fat interface, numerous little interfaces are preferred based on groups of methods with each interface serving one submodule“.

Let us break down the above definition into two parts.

  1. First, no class should be forced to implement any method(s) of an interface they don’t use.
  2. Secondly, instead of creating a large or you can say fat interfaces, create multiple smaller interfaces with the aim that the clients should only think about the methods that are of interest to them.

As per the Single Responsibility Principle of SOLID, like classes, interfaces also should have a single responsibility. That means we shouldn’t force any class to implement any method(s) which they don’t require.

Let us understand the Interface Segregation Principle in C# with an example.

Please have a look at the following diagram.

Interface Segregation Principle in C# with an example

As you can see in the above diagram, we have an interface i.e. IPrinterTasks declared with four methods. Now if any class want to implement this interface then that class should have to provide the implementation to all the four methods of IPrinterTasks interface. As you can see in the above diagram, we have two classes HPLaserJetPrinter and LiquidInkjetPrinter who want the printer service.

But the requirement is the HPLaserJetPrinter want all the services provided by the IPrinterTasks while the LiquidInkjetPrinter wants only the  Print and Scan service of the printer. As we have declared all the methods within the IPrinterTasks interface, then it is mandatory for the LiquidInkjetPrinter class to provide implementation to Scan and Print methods along with the Fax and PrinctDulex method which are not required by the class.

Example without using the Interface Segregation Principle:

IPrinterTasks.cs

namespace SOLID_PRINCIPLES.ISP
{
    public interface IPrinterTasks
    {
        void Print(string PrintContent);
        void Scan(string ScanContent);
        void Fax(string FaxContent);
        void PrintDuplex(string PrintDuplexContent);
    }
}

HPLaserJetPrinter.cs

namespace SOLID_PRINCIPLES.ISP
{
    public class HPLaserJetPrinter : IPrinterTasks
    {
        public void Print(string PrintContent)
        {
            Console.WriteLine("Print Done");
        }

        public void Scan(string ScanContent)
        {
            Console.WriteLine("Scan content");
        }

        public void Fax(string FaxContent)
        {
            Console.WriteLine("Fax content");
        }

        public void PrintDuplex(string PrintDuplexContent)
        {
            Console.WriteLine("Print Duplex content");
        }
    }
}

LiquidInkjetPrinter.cs

namespace SOLID_PRINCIPLES.ISP
{
    class LiquidInkjetPrinter : IPrinterTasks
    {
        public void Print(string PrintContent)
        {
            Console.WriteLine("Print Done");
        }

        public void Scan(string ScanContent)
        {
            Console.WriteLine("Scan content");
        }

        public void Fax(string FaxContent)
        {
            throw new NotImplementedException();
        }

        public void PrintDuplex(string PrintDuplexContent)
        {
            throw new NotImplementedException();
        }
    }
}

As you can see in the above LiquidInkjetPrinter class the Fax and PrintDuplex methods are not required by the class but, still, it is implementing these two methods. This is violating the Interface Segregation Principle in C# as we are forcing the class to implement two methods which they don’t require.

Example using the Interface Segregation Principle:

Please have a look at the following diagram.

Interface Segregation Principle in C#

As you can see in the above diagram, now we have split that big interface into three small interfaces. Each interface now having some specific purpose. Now if any class wants all the services then that class needs to implement all the three interfaces as shown below.

Interface Segregation Principle in C#Now, if any class wants the Scan and Print service, then that class needs to implement only the IPrinterTasks interfaces as shown in the below image.

Interface Segregation Principle in C#

The Complete Code is given below:
namespace SOLID_PRINCIPLES.ISP
{
    public interface IPrinterTasks
    {
        void Print(string PrintContent);
        void Scan(string ScanContent);
    }
    interface IFaxTasks
    {
        void Fax(string content);
    }
    interface IPrintDuplexTasks
    {
        void PrintDuplex(string content);
    }
}

namespace SOLID_PRINCIPLES.ISP
{
    public class HPLaserJetPrinter : IPrinterTasks, IFaxTasks, 
                                     IPrintDuplexTasks
    {
        public void Print(string PrintContent)
        {
            Console.WriteLine("Print Done");
        }

        public void Scan(string ScanContent)
        {
            Console.WriteLine("Scan content");
        }

        public void Fax(string FaxContent)
        {
            Console.WriteLine("Fax content");
        }

        public void PrintDuplex(string PrintDuplexContent)
        {
            Console.WriteLine("Print Duplex content");
        }
    }
}
namespace SOLID_PRINCIPLES.ISP
{
    class LiquidInkjetPrinter : IPrinterTasks
    {
        public void Print(string PrintContent)
        {
            Console.WriteLine("Print Done");
        }

        public void Scan(string ScanContent)
        {
            Console.WriteLine("Scan content");
        }
    }
}

In the next article, I am going to discuss the Dependency Inversion principle in C# with a real-time example. Here, in this article, I try to explain the Interface Segregation Principle with a real-time example. I hope you understood the need and use of the Interface Segregation Principle.

1 thought on “Interface Segregation Principle in C#”

Leave a Reply

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