Interface Segregation Principle

'Clients should not be forced to depend upon interfaces having methods that client do not need.'

This principle is closely related to Single Responsibility Principle as it supports the idea of having a specific interface than a generic purpose interface (known as 'Fat Interface' or 'Polluted Interface').

If we have a single interface having various functionality, a child implementing that interface will implement desired method along with all the other unwanted methods of that interface for no reason.
An interface declaring much more work should be break down to small interfaces that specifies its purpose.
This allows the child class to inherit the interface it wants, to achieve a purpose.

For eg:
   public interface DocumentGenerator{
      public void abstract generateWordDoc( );

      public void abstract generatePdf( );

      public void abstract generateTxt( );
   }

   class OutBound implements DocumentGenerator {
      public void generateWordDoc( ) {
       //using to print document in word format
      }

      public void generatePdf( ) {
      }

      public void generateTxt( ) {
      }
   }

As per Interface Segregation Principle,
   public interface PDFGenerator{
      public void abstract generatePdf( );
   }

   public interface WordDocumentGenerator {
      public void abstract generateWordDoc( );
   }

   class OutBound implements DocumentGenerator {
      public void generateWordDoc( ) {
          //using to print document in word format
      }
   }

Comments

Popular posts from this blog

@Overrride annotation introduced in Java1.5

Liskov Substitution Principle (LSP)