top of page

Interface Segregation Principle Using C#


When we design/develop a class, that class should not implement any such interfaces which are not required by the customer even if it is related to that class which we are designing or developing.

Let us see with an example, in which we'll sevelop a calculator for school, shops and for programmers.

When we say calculator many things will come into mind i.e. add, sub, div, multiplication, Trigonometry functions, Calculus, etc.

Below is the code which fulfills the requirement all 3 kinds of users mentioned above i.e. School, Shops and for Programmers

using System;  
using System.Linq;  
 
namespace Solid.Principle.ISP.Demo  
{  
 public class Calculator  
    {  
 /// <summary> 
 /// This Method is for all kind of users 
 /// </summary> 
 /// <param name="nums"></param> 
 /// <returns></returns> 
 public int Add(params int[] nums)  
        {  
 return nums.Sum();  
        }  
 
 /// <summary> 
 /// This Method is for all kind of users 
 /// </summary> 
 /// <param name="a"></param> 
 /// <param name="b"></param> 
 /// <returns></returns> 
 public int SubStract(int a, int b)  
        {  
 return a > b ? a - b : b - a;  
        }  
 
 /// <summary> 
 /// This Method is for all kind of users 
 /// </summary> 
 /// <param name="nums"></param> 
 /// <returns></returns> 
 public int Multplicaton(params int[] nums)  
        {  
 int result = 0;  
            nums.ToList().ForEach(a => result = result * a);  
 return result;  
        }  
 
 /// <summary> 
 /// This Method is for all kind of users 
 /// </summary> 
 /// <param name="a"></param> 
 /// <param name="b"></param> 
 /// <returns></returns> 
 public int Division(int a, int b)  
        {  
 return a > b ? a/b : b/a;  
        }  
 
 /// <summary> 
 /// This Method is for School students 
 /// </summary> 
 /// <param name="x"></param> 
 /// <returns></returns> 
 public double SinOf(double x)  
        {  
 return Math.Sin(x);  
        }  
 
 /// <summary> 
 /// This Method is for School students 
 /// </summary> 
 /// <param name="x"></param> 
 /// <returns></returns> 
 public double CosOf(double x)  
        {  
 return Math.Cos(x);  
        }  
 
 /// <summary> 
 /// This Method is for School students 
 /// </summary> 
 /// <param name="x"></param> 
 /// <returns></returns> 
 public double TanOf(double x)  
        {  
 return Math.Tan(x);  
        }  
 
 /// <summary> 
 /// This method is for Programmer 
 /// </summary> 
 /// <param name="input"></param> 
 /// <returns></returns> 
 public string ConvertHexToBinary(string input)  
        {  
 return $"Add logic to Convert to Binary for hex input : {input}";  
        }  
 
 /// <summary> 
 ///  /// <summary> 
 /// This method is for Programmer 
 /// </summary> 
 /// <param name="input"></param> 
 /// <returns></returns> 
 /// </summary> 
 /// <param name="input"></param> 
 /// <returns></returns> 
 public string ConvertBinaryToHex(string input)  
        {  
 return $"Add logic to Convert to hex for Binary input : {input}";  
        }  
    }  
}   

But if we observe the above code we can understand that we are giving some extra features which users may not be interested in (as the user won’t use it). For example, a shop holder needs only common functions like add, subtract, multiply, divide. this user doesn’t need Trigonometry functions and HexToBinary and Vice versa functions.

And school students need Common functions + Trigonometry functions + may need or may not need HexToBinary and Vice versa function.

But as we designed and developed class including all together we don’t have the option for segregation here. We need to deliver all features to end-user.


At this stage we ISP will play a key role. We can split the Calculator class into the below interface (Contracts).

namespace Solid.Principle.ISP.Demo.Contract  
{  
 /// <summary> 
 /// This Interface is for all kind of users 
 /// </summary> 
 public interface ICommonCalci  
    {  
 int Add(params int[] nums);  
 int SubStract(int a, int b);  
 int Multplicaton(params int[] nums);  
 int Division(int a, int b);  
    }  
}  

namespace Solid.Principle.ISP.Demo.Contract  
{  
 /// <summary> 
 /// This Interface is for School/College students 
 /// </summary> 
 public interface ITrigonometryCalci  
    {  
 double SinOf(double x);  
 double CosOf(double x);  
 double TanOf(double x);  
    }  
}   

namespace Solid.Principle.ISP.Demo.Contract  
{  
 /// <summary> 
 /// This Interface is for Programmer 
 /// </summary> 
 public interface IProgrammerCalci  
    {  
 string ConvertHexToBinary(string input);  
 string ConvertBinaryToHex(string input);  
    }  
}  

And below is the newly designed calculator class by inheriting all 3 interfaces. Based on user needs we can deliver the particular interface(s) to the user.

using Solid.Principle.ISP.Demo.Contract;  
using System;  
using System.Linq;  
 
namespace Solid.Principle.ISP.Demo  
{  
 public class Calculator : ICommonCalci, ITrigonometryCalci, IProgrammerCalci  
    {  
 /// <summary> 
 /// This Method is for all kind of users 
 /// </summary> 
 /// <param name="nums"></param> 
 /// <returns></returns> 
 public int Add(params int[] nums)  
        {  
 return nums.Sum();  
        }  
 
 /// <summary> 
 /// This Method is for all kind of users 
 /// </summary> 
 /// <param name="a"></param> 
 /// <param name="b"></param> 
 /// <returns></returns> 
 public int SubStract(int a, int b)  
        {  
 return a > b ? a - b : b - a;  
        }  
 
 /// <summary> 
 /// This Method is for all kind of users 
 /// </summary> 
 /// <param name="nums"></param> 
 /// <returns></returns> 
 public int Multplicaton(params int[] nums)  
        {  
 int result = 0;  
            nums.ToList().ForEach(a => result = result * a);  
 return result;  
        }  
 
 /// <summary> 
 /// This Method is for all kind of users 
 /// </summary> 
 /// <param name="a"></param> 
 /// <param name="b"></param> 
 /// <returns></returns> 
 public int Division(int a, int b)  
        {  
 return a > b ? a/b : b/a;  
        }  
 
 /// <summary> 
 /// This Method is for School students 
 /// </summary> 
 /// <param name="x"></param> 
 /// <returns></returns> 
 public double SinOf(double x)  
        {  
 return Math.Sin(x);  
        }  
 
 /// <summary> 
 /// This Method is for School students 
 /// </summary> 
 /// <param name="x"></param> 
 /// <returns></returns> 
 public double CosOf(double x)  
        {  
 return Math.Cos(x);  
        }  
 
 /// <summary> 
 /// This Method is for School students 
 /// </summary> 
 /// <param name="x"></param> 
 /// <returns></returns> 
 public double TanOf(double x)  
        {  
 return Math.Tan(x);  
        }  
 
 /// <summary> 
 /// This method is for Programmer 
 /// </summary> 
 /// <param name="input"></param> 
 /// <returns></returns> 
 public string ConvertHexToBinary(string input)  
        {  
 return $"Add logic to Convert to Binary for hex input : {input}";  
        }  
 
 /// <summary> 
 ///  /// <summary> 
 /// This method is for Programmer 
 /// </summary> 
 /// <param name="input"></param> 
 /// <returns></returns> 
 /// </summary> 
 /// <param name="input"></param> 
 /// <returns></returns> 
 public string ConvertBinaryToHex(string input)  
        {  
 return $"Add logic to Convert to hex for Binary input : {input}";  
        }  
    }  
}  

As per the above code, we can see that

  • For Shop holders we can provide an interface ICommonCalci

  • For Programmers we can provide ICommonCalci + IProgrammerCalci.

  • For Students we can provide ICommonCalci + ITrigonometryCalci + IProgrammerCalci (Based on Requirement)


This way we fulfilled all user requirements. And still this class is feasible to extend with new interfaces for new kinds of users and for new kinds of requirements.

Summary

In this article, we learned about the interface segregation principle. I attached the source code for this article. download and extend it for better understanding.


Source: C# Corner


The Tech Platform

0 comments

Comentários


bottom of page