Interface Segregation Principle Using Java

Introduction

 
In OOP (Object Oriented Programming) languages the solid principle plays a desired design role to make a class flexible and easy to understand. Today we will see the Interface segregation principle.
 

Why ISP?

 
When we design/develop a class, that class should not implement any such function which is not required by the customer even those related classes that we are designing or developing.
 
Let us see with an example of developing a calculator for school, shops, and for programmers.
 
When we say calculator many things will come into your mind i.e. addition, subtraction, division, multiplication, Trigonometry functions, Calculus etc.
 
Below is the code which fulfills the requirement of all 3 kinds of users mentioned above.
  1. public class Calculator {  
  2.   
  3.     /** <summary> 
  4.     * This Method is for all kind of users 
  5.     * </summary> 
  6.     * <param name="nums"></param> 
  7.     * <returns></returns> 
  8.      */  
  9.     public int add(int[] nums) {  
  10.         int sum = 0;  
  11.         //Advanced for loop  
  12.         for (int num : nums) {  
  13.             sum = sum + num;  
  14.         }  
  15.         return sum;  
  16.     }  
  17.   
  18.   
  19.     /** <summary> 
  20.      * This Method is for all kind of users 
  21.      * </summary> 
  22.      * <param name="nums"></param> 
  23.      * <returns></returns> 
  24.      */  
  25.     public int substract(int a, int b) {  
  26.         return a > b ? a - b : b - a;  
  27.     }  
  28.   
  29.     /** <summary> 
  30.      * This Method is for all kind of users 
  31.      * </summary> 
  32.      * <param name="nums"></param> 
  33.      * <returns></returns> 
  34.      */  
  35.     public int multplicaton(int[] nums) {  
  36.         int mul = 0;  
  37.         //Advanced for loop  
  38.         for (int num : nums) {  
  39.             mul = mul * num;  
  40.         }  
  41.         return mul;  
  42.     }  
  43.   
  44.     /** <summary> 
  45.      * This Method is for all kind of users 
  46.      * </summary> 
  47.      * <param name="nums"></param> 
  48.      * <returns></returns> 
  49.      */  
  50.     public int division(int a, int b) {  
  51.         return a > b ? a / b : b / a;  
  52.     }  
  53.   
  54.   
  55.     /** <summary> 
  56.      * This Method is for School students 
  57.      * </summary> 
  58.      * <param name="nums"></param> 
  59.      * <returns></returns> 
  60.      */  
  61.     public double sinOf(double x) {  
  62.         return Math.sin(x);  
  63.     }  
  64.   
  65.     /** <summary> 
  66.      * This Method is for School students 
  67.      * </summary> 
  68.      * <param name="nums"></param> 
  69.      * <returns></returns> 
  70.      */  
  71.     public double cosOf(double x) {  
  72.         return Math.cos(x);  
  73.     }  
  74.   
  75.     /** <summary> 
  76.      * This Method is for School students 
  77.      * </summary> 
  78.      * <param name="nums"></param> 
  79.      * <returns></returns> 
  80.      */  
  81.     public double tanOf(double x) {  
  82.         return Math.tan(x);  
  83.     }  
  84.   
  85.     /** <summary> 
  86.      * This Method is for Programmer 
  87.      * </summary> 
  88.      * <param name="nums"></param> 
  89.      * <returns></returns> 
  90.      */  
  91.     public String convertHexToBinary(String input) {  
  92.         return "Add logic to Convert to Binary for hex input :" + "input";  
  93.     }  
  94.   
  95.     /** <summary> 
  96.      * This Method is for Programmer 
  97.      * </summary> 
  98.      * <param name="nums"></param> 
  99.      * <returns></returns> 
  100.      */  
  101.     public String convertBinaryToHex(String input) {  
  102.         return "Add logic to Convert to hex for Binary input :" + "input";  
  103.     }  
  104. }  
But if we observe the above code we can understand that we are giving some extra features which users may not interested in (asin,  the user won’t use it). For example, a shop holder need only common functions like add, sub, mult, div. 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 classes including all together we don’t have an option for segregation here. We need to deliver all features to the end-user.
 
At this stage our ISP will play key role. We can split Calculator class into the below interface (Contracts). 
  1. /** 
  2.  * This Interface is for all kind of users 
  3.  */  
  4. public interface ICommonCalci {  
  5.     int add(int[] nums);  
  6.     int substract(int a, int b);  
  7.     int multplicaton(int[] nums);  
  8.     int division(int a, int b);  
  9. }  
  1. /** 
  2.  * This Interface is for Programmer 
  3.  */  
  4. public interface IProgrammerCalci {  
  5.     String convertHexToBinary(String input);  
  6.     String convertBinaryToHex(String input);  
  7. }  
  1. /** 
  2.  * This Interface is for School/College students 
  3.  */  
  4. public interface ITrigonometryCalci {  
  5.     double sinOf(double x);  
  6.     double cosOf(double x);  
  7.     double tanOf(double x);  
  8. }  
 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.  
  1. import Contract.ICommonCalci;  
  2. import Contract.IProgrammerCalci;  
  3. import Contract.ITrigonometryCalci;  
  4.   
  5. public class Calculator implements ICommonCalci, ITrigonometryCalci, IProgrammerCalci {  
  6.   
  7.     /** <summary> 
  8.     * This Method is for all kind of users 
  9.     * </summary> 
  10.     * <param name="nums"></param> 
  11.     * <returns></returns> 
  12.      */  
  13.     public int add(int[] nums) {  
  14.         int sum = 0;  
  15.         //Advanced for loop  
  16.         for (int num : nums) {  
  17.             sum = sum + num;  
  18.         }  
  19.         return sum;  
  20.     }  
  21.   
  22.   
  23.     /** <summary> 
  24.      * This Method is for all kind of users 
  25.      * </summary> 
  26.      * <param name="nums"></param> 
  27.      * <returns></returns> 
  28.      */  
  29.     public int substract(int a, int b) {  
  30.         return a > b ? a - b : b - a;  
  31.     }  
  32.   
  33.     /** <summary> 
  34.      * This Method is for all kind of users 
  35.      * </summary> 
  36.      * <param name="nums"></param> 
  37.      * <returns></returns> 
  38.      */  
  39.     public int multplicaton(int[] nums) {  
  40.         int mul = 0;  
  41.         //Advanced for loop  
  42.         for (int num : nums) {  
  43.             mul = mul * num;  
  44.         }  
  45.         return mul;  
  46.     }  
  47.   
  48.     /** <summary> 
  49.      * This Method is for all kind of users 
  50.      * </summary> 
  51.      * <param name="nums"></param> 
  52.      * <returns></returns> 
  53.      */  
  54.     public int division(int a, int b) {  
  55.         return a > b ? a / b : b / a;  
  56.     }  
  57.   
  58.   
  59.     /** <summary> 
  60.      * This Method is for School students 
  61.      * </summary> 
  62.      * <param name="nums"></param> 
  63.      * <returns></returns> 
  64.      */  
  65.     public double sinOf(double x) {  
  66.         return Math.sin(x);  
  67.     }  
  68.   
  69.     /** <summary> 
  70.      * This Method is for School students 
  71.      * </summary> 
  72.      * <param name="nums"></param> 
  73.      * <returns></returns> 
  74.      */  
  75.     public double cosOf(double x) {  
  76.         return Math.cos(x);  
  77.     }  
  78.   
  79.     /** <summary> 
  80.      * This Method is for School students 
  81.      * </summary> 
  82.      * <param name="nums"></param> 
  83.      * <returns></returns> 
  84.      */  
  85.     public double tanOf(double x) {  
  86.         return Math.tan(x);  
  87.     }  
  88.   
  89.     /** <summary> 
  90.      * This Method is for Programmer 
  91.      * </summary> 
  92.      * <param name="nums"></param> 
  93.      * <returns></returns> 
  94.      */  
  95.     public String convertHexToBinary(String input) {  
  96.         return "Add logic to Convert to Binary for hex input :" + "input";  
  97.     }  
  98.   
  99.     /** <summary> 
  100.      * This Method is for Programmer 
  101.      * </summary> 
  102.      * <param name="nums"></param> 
  103.      * <returns></returns> 
  104.      */  
  105.     public String convertBinaryToHex(String input) {  
  106.         return "Add logic to Convert to hex for Binary input :" + "input";  
  107.     }  
  108. }  
As per the above code, we can see that, 
  • For Shop holders we can provide 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 source code for this article. Download and extend it for better understanding.