SOLID Principles: Interface Segregation Principle (ISP)

Hussein Reda
Oct 25, 2020
  • Developers shouldn’t be forced to depend upon interfaces that they don’t use.
  • Example:
public interface someInterface{public int methodOne(){
// Do something
}public int methodTwo(){
// Do somthing diffrent
}}

if you want to implement this Interface

public someClass implements someInterface{public int methodOne(){
// logic to do something
}public int methodTwo(){
}
}

and only need to overwrite one method only why you forced to overwrite the other method and left it empty.

we solve this problem by creating two separate interfaces, every interface has only relevant methods.

public interface someInterface{public int methodOne(){
// Do something
}}public interface someDiffrentInterface{public int methodTwo(){
// Do somthing diffrent
}}

Now you can implement the only interface you need.

depending on something that carries baggage that you don’t need can cause you troubles that you didn’t expect.

continuo to the rest of SOLID

--

--