Tutorialspoint examples

Learn java tutorial with examples for beginners online

Interface Segregation Principle design isp java


The object oriented design principles are used to achieve highly cohesive and loosely couple solution. Design principles provides us a way to create the clean and modular design.

The SOLID principles of Object Oriented Design include following five principles:

  • Single Responsibility Principle (SRP)
  • Open Closed Design Principle (OCP)
  • Liskov Substitution Principle (LSP)
  • Interface Segregation Principle (ISP)
  • Dependency Injection or Inversion principle (DIP)

Interface Segregation Principle (ISP)

It is a SOLID design principle and represent “I” on the SOLID acronym. According to ISP clients should not be forced to implement unnecessary methods which they will not use. To achieve the ISP SOLID design principle we favor many, smaller, client-specific interfaces over one larger interface.

Let us understand the interface segregation principle by below example. In below example we have one interface which have two methods to generate reports in different formats generateExcel()and generatePdf().

public interface GenerateReport{
  public void generateExcel();
  public void generatePDF();
}

Now consider a case client Test wants to use this interface but want to use reports only in PDF format and not in excel. With the above design he cannot achieve it because we are forcing him to implement both methods. So this design is not following the interface segregation principle.

Better Solution:
We can break the GenerateReport interface into two small interfaces which contains separate methods and client can use the desire method.
 

Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status