Interface Segregation Principle(ISP)

ISP Image

Interface Segregation Principle

The Interface Segregation Principle(ISP) deals with the disadvantages of “fat” interfaces.

The Interface Segregation Principle(ISP) states that no client code object should be forced to depend on methods it does not use. Basically, each code object should only implement what it needs, and not be required to implement anything else.

The ISP was first used and formulated by Robert C. Martin while consulting for Xerox.

I will tackle today’s principle using an example from MSDN

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
 
namespace SOLIDPrinciplesDemo 
{ 
    // Only common generic methods exists for all derived classes. 
    interface IDataProvider 
    { 
        int OpenConnection(); 
        int CloseConnection(); 
    } 
    // Implement methods specific to the respective derived classe 
    interface ISqlDataProvider : IDataProvider 
    { 
        int ExecuteSqlCommand(); 
    } 
    // Implement methods specific to the respective derived classe 
    interface IOracleDataProvider : IDataProvider 
    { 
        int ExecuteOracleCommand(); 
    } 
    // Client 1 
    // Should not force SqlDataProvider client to implement ExecuteOracleCommand, as it wont required that method to be implemented. 
    // So that we will derive ISqlDataProvider not IOracleDataProvider 
    class SqlDataClient : ISqlDataProvider 
    { 
        public int OpenConnection() 
        { 
            Console.WriteLine("\nSql Connection opened successfully"); 
            return 1; 
        } 
        public int CloseConnection() 
        { 
            Console.WriteLine("Sql Connection closed successfully"); 
            return 1; 
        } 
 
        // Implemeting ISqlDataProvider, we are not forcing the client to implement IOracleDataProvider 
        public int ExecuteSqlCommand() 
        { 
            Console.WriteLine("Sql Server specific Command Executed successfully"); 
            return 1; 
        } 
    } 
    // Client 2 
    // Should not force OracleDataProvider client to implement ExecuteSqlCommand, as it wont required that method to be implemented. 
    // So that we will derive IOracleDataProvider not ISqlDataProvider 
    class OracleDataClient : IOracleDataProvider 
    { 
        public int OpenConnection() 
        { 
            Console.WriteLine("\nOracle Connection opened successfully"); 
            return 1; 
        } 
        public int CloseConnection() 
        { 
            Console.WriteLine("Oracle Connection closed successfully"); 
            return 1; 
        } 
        // Implemeting IOracleDataProvider, we are not forcing the client to implement ISqlDataProvider 
        public int ExecuteOracleCommand() 
        { 
            Console.WriteLine("Oracle specific Command Executed successfully"); 
            return 1; 
        } 
    } 
    class InterfaceSegregationPrincipleDemo 
    { 
        public static void ISPDemo() 
        { 
            Console.WriteLine("\n\nInterface Inversion Principle Demo "); 
 
            // Each client will implement their respective methods no base class forces the other client to implement the methods which dont required. 
            // From the above implementation, we are not forcing Sql client to implemnt orcale logic or Oracle client to implement sql logic. 
 
            ISqlDataProvider SqlDataProviderObject = new SqlDataClient(); 
            SqlDataProviderObject.OpenConnection(); 
            SqlDataProviderObject.ExecuteSqlCommand(); 
            SqlDataProviderObject.CloseConnection(); 
 
            IOracleDataProvider OracleDataProviderObject = new OracleDataClient(); 
            OracleDataProviderObject.OpenConnection(); 
            OracleDataProviderObject.ExecuteOracleCommand(); 
            OracleDataProviderObject.CloseConnection(); 
        } 
    } 
}

Advantages of Using ISP:

  1. Better Understandability
  2. Better Maintainability
  3. High Cohesion
  4. Low Coupling

Limitations

ISP like any other principle should be used intelligently when necessary otherwise it will result in a code containing lots of interfaces containing one method. So the decision should be taken intelligently.

Additional Resources

1.Agile Software Development, Principles, Patterns, and Practices-Robert C.Martin

2.SOLID Principles of Object Oriented Design– Steve Smith(Pluralsight)

3.Principles Of OOD

That’s it folks for this week,hope you learnt something in today’s topic,Interface Segregation Principle(ISP), the “I” in the S.O.L.I.D acronym,i will be concluding our series in the next post covering the Dependency Inversion Principle.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.