Usability of SOLID Principles with Kotlin - Interface Segregation Principle: Part 4
- 1 de jun. de 2024
- 2 min de leitura
We return to our article, now entering the fourth principle of SOLID, the Interface Segregation Principle.

The Interface Segregation Principle (ISP). Introduced by Robert C. Martin, also known as Uncle Bob, the ISP focuses on creating specific interfaces, avoiding generic interfaces that might be implemented by classes that do not use all of their methods. In other words, we should not have interfaces with multiple methods, but rather smaller interfaces where it makes sense for certain classes to implement and use all their methods. In this way, we have more concise and easier-to-maintain interfaces.
Importance of ISP
Reduces Coupling: Specific and cohesive interfaces ensure that classes depend only on the methods they really need. This reduces coupling and facilitates the maintenance and evolution of the code.
Eases Understanding: Smaller, specific interfaces are easier to understand and implement because each has a clear and defined purpose.
Promotes Reuse: By dividing large interfaces into several smaller interfaces, you allow different parts of the system to reuse only the necessary interfaces.
Facilitates Testing: Classes that depend on smaller interfaces are easier to test in isolation, as each specific interface can be mocked individually.
Let's consider an example where we have a vehicle control system and a genereci Interface with Multiple Methods:
interface Vehicle {
fun startEngine()
fun stopEngine()
fun fly()
fun sail()
}
In this example, a class that implements Vehicle will be forced to implement methods that may not be relevant to it. For instance, a car does not need the fly() or sail() method.
Applying ISP
Let's divide the Vehicle interface into several more specific interfaces:
interface Engine {
fun startEngine()
fun stopEngine()
}
interface Flyable {
fun fly()
}
interface Sailable {
fun sail()
}
Now, we can create classes that implement only the necessary interfaces.
Implementation of Specific Classes
class Car : Engine {
override fun startEngine() {
println("Car engine started.")
}
override fun stopEngine() {
println("Car engine stopped.")
}
}
class Airplane : Engine, Flyable {
override fun startEngine() {
println("Airplane engine started.")
}
override fun stopEngine() {
println("Airplane engine stopped.")
}
override fun fly() {
println("Airplane is flying.")
}
}
class Boat : Engine, Sailable {
override fun startEngine() {
println("Boat engine started.")
}
override fun stopEngine() {
println("Boat engine stopped.")
}
override fun sail() {
println("Boat is sailing.")
}
}
In this example, each class implements only the interfaces whose methods are relevant to the class. The Car implements only the methods of Engine, the Airplane implements methods of Engine and Flyable, and the Boat implements methods of Engine and Sailable.
Conclusion
The Interface Segregation Principle (ISP) promotes the creation of specific and cohesive interfaces, improving the modularity, maintainability, and clarity of the code. By applying ISP, we avoid the implementation of unnecessary methods, reducing coupling and facilitating component reuse. In Kotlin, the ISP can be efficiently applied, resulting in cleaner and easier-to-understand code.
Comments