Click here to Skip to main content
15,888,733 members
Articles
Tip/Trick
(untagged)

Implementing Interface Segregation Principle of S.O.L.I.D

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
20 Feb 2011CPOL1 min read 22.1K   6   7
Implementing Interface Segregation Principle of S.O.L.I.D
Introduction

This is a sincere attempt to make the audience understand the importance of Interface Segregation principal of SOLID, and how it can be useful and implemented.

Background

Couple of times, we see that we are bound to implement interfaces which are fat, what I mean is we have to implement the methods which we really don't need or would be of any help to us.

This coding tip explains how we can really identify a fat interface and refactor the code in order to make the interface thin without really breaking the application.

Using the Code

Let's do step by step refactoring to make the whole thing look simpler and easy to understand.

We have an interface called IDataOperation which is designed to perform lot many things.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SOLID
{
   public interface IDataOperation 
    {
        void saveData(string name);
        bool connectDataBase();
        void  createReportData();
    }
}

Now we have a concrete class called ReportAccess which implements IDataOperation.

You can see below that the class requires only createreportData method, but it had to implement all the irrelevant methods also:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SOLID
{
    class ReportAccess :IDataOperation     
    {

        #region IDataOperation Members

        public void saveData(string name)
        {
            Console.WriteLine("Saving Data..");
        }

        public bool connectDataBase()
        {
            throw new NotImplementedException();
        }

        public void createReportData()
        {
            Console.WriteLine("Created report data...");
        }

        #endregion
    }
}


Refactoring Begins .....

We can refactor the interface IDataOperation and separate the method CreateDataReport and place it into a new interface called IReportOperation.

After refactoring, the code will look like the below snippet:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SOLID
{
   public interface IDataOperation 
    {
        void saveData(string name);
        bool connectDataBase();
        void  createReportData();
    }

   public interface IReportOperation
   {
       void createReportData();
   }
}

In order to prevent the application from breaking, what we can do is derive IDataOperation from IReportOperation and code now would look like this ..

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SOLID
{
   public interface IDataOperation : IReportOperation
    {
        void saveData(string name);
        bool connectDataBase();
        void  createReportData();
    }

   public interface IReportOperation
   {
       void createReportData();
   }
} 


On the other hand, we can also use only the IReportOperation interface in the concrete class ReportAccess and get rid of irrelevant methods so after refactoring the class ReportAccess the code would look like this:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SOLID
{
    class ReportAccess :IReportOperation
    {
        #region IDataOperation Members

        //public void saveData(string name)
        //{
        //    Console.WriteLine("Saving Data..");
        //}

        //public bool connectDataBase()
        //{
        //    throw new NotImplementedException();
        //}

        public void createReportData()
        {
            Console.WriteLine("Created report data...");
        }

        #endregion
    }
}

The commented out sections are the methods which no longer need to be implemented or bothered about:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SOLID
{
    class Program
    {
        static void Main(string[] args)
        {
            ReportAccess rp = new ReportAccess();
            //rp.saveData("saurav");
            rp.createReportData();
        }       
    }
}

Finally, we have our main method.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Computer geek very much interested in learning and adopting latest and cutting edge technologies .

Expertise in asp.net,c#,WCF,web services,Jquery,MVC ,TTD's and design principles and patterns applying SOLID

Designation - Architect
This is a Organisation

9 members

Comments and Discussions

 
SuggestionMissed the idea Pin
ArchAngel12329-Oct-14 6:40
ArchAngel12329-Oct-14 6:40 
It's not just you - a lot of people misunderstand the underlying principle

Interface segregation has absolutely nothing to do with whether YOU don't want to have to implement all pieces of an interface - it's not about your laziness or immediate short term usage pattern you have planned for the interface.

It's a principle that instructs creating granular interfaces - e.g., my washing machine interface shouldn't also define microwave oven functionality (might be great if I have a very special washing machine/microwave combination that lets me make popcorn while I wait for my laundry, but overall it's a poor interface design)

BUT - a properly designed interface defines ALL the attributes necessary to fully express the properties, behavior, and actions involved with that interface - no more, but no LESS

So creating arbitrary slices as you did of otherwise properly designed interfaces (your starting with a poor interface example obfuscates this) just so you don't have to implement all the pieces (but others who might need them all now have to gang all those arbitrarily tiny interfaces together) is poor design and not what the interface segregation principle intends.

Take IList for example - you might only need to use Add and [] but you have to implement all the other methods and properties in order to be fully exchangeable
GeneralRe: Missed the idea Pin
Ahmer Bashir7-Apr-15 19:15
professionalAhmer Bashir7-Apr-15 19:15 
GeneralMy vote of 2 Pin
squarenixx@msn.com13-Oct-12 8:56
squarenixx@msn.com13-Oct-12 8:56 
GeneralReason for my vote of 2 Not really describing the principle Pin
Anders Lybecker21-Feb-11 18:22
Anders Lybecker21-Feb-11 18:22 
GeneralGood stuff keep it up Pin
pcamit20-Feb-11 5:54
pcamit20-Feb-11 5:54 
Generalthanks a lot buddy Pin
imsauravroy20-Feb-11 2:13
imsauravroy20-Feb-11 2:13 
GeneralPublished and updated your Tip a little (just formatting). ... Pin
Sandeep Mewara20-Feb-11 2:01
mveSandeep Mewara20-Feb-11 2:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.