Interface segregation principle

By | February 24, 2020

Interface segregation principle

Many small and dedicated interfaces are better than few and general.

The purpose of this rule is nonexistence interfaces, which do many things and has many responsibilities. Wrong projected interfaces have a tendency to grow up too much. They are becoming incoherent. Classes, which implement these interfaces are incoherent too, and they must do, what they do not need. They depend on the methods they do not use.

This rule can be applied to methods, functions, classes, modules and packages.

Bestseller dnia w księgarni Złote Myśli

Advantages

  • Easier to ensure the Liskov substitution principle and the Single responsibility principle.
  • Easier code maintenance.
  • Greater code readability.
  • Easier code change.
  • No need to implement unnecessary methods in classes that should not implement them.
  • No God-interfaces.
  • The code is less dependent on another code.

Disadvantages

  • Many small interfaces.
  • Greater effort during software development.

Examples

  • Interface IAnimal had method Run. Classes Dog and Fish implement this interface. In class Fish method Run is unnecessary.
  • Interface File has two methods: Read and Write. Class FileOnlyToRead implements this interface. Method Write in unnecessary or do something different what this method name informs us (it can not write because it is read-only file).

All posts from mini project: Learn SOLID and OOP principles:

Sources

Main image

Materials

Leave a Reply

Your email address will not be published. Required fields are marked *