ISP - Interface Segregation Principle
The Interface Segregation Principle is the fourth principle in the Solid Design Principles.
- Clients should not be forced to depend on interfaces they do not use.
Violating ISP:
protocol Worker {
func eat()
func work()
}
class HumanWorker: Worker {
func eat() {
print("Human is eating...")
}
func work() {
print("Human is working...")
}
}
class RobotWorker: Worker {
func work() {
print("Robot is working...")
}
func eat() {
fatalError("Robot can't eat...")
}
}
// usage
let robot = RobotWorker()
robot.eat() // This will cause a runtime error
Issues with Violating ISP:
- Unnecessary Methods:
- Classes are forced to implement methods they do not use, leading to potential runtime errors.
- Increased Complexity:
- Interfaces become bloated with methods irrelevant to some implementations.
- Reduced Maintainability:
- Changes to interfaces affect all implementing classes, even if they do not use the changed methods.
Adhering to ISP
To adhere to ISP, separate the protocol into small pieces:
protocol Workable {
func work()
}
protocol Eatable {
func eat()
}
protocol WorkerISP: Workable, Eatable {}
class HumanWorkerISP: WorkerISP {
func eat() {
print("HumanWorkerISP is eating...")
}
func work() {
print("HumanWorkerISP is working...")
}
}
class RobotWorkerISP: Workable {
func work() {
print("RobotWorkerISP is eating...")
}
}
// usage
let humanWorker = HumanWorkerISP()
humanWorker.eat()
humanWorker.work()
let robotWorker = RobotWorkerISP()
robotWorker.work()
Benefits of Adhering to ISP:
- Improved Maintainability:
- Smaller, specific interfaces are easier to understand and maintain.
- Enhanced Flexibility:
- Changes to an interface do not affect clients that do not use that part of the interface.
- Increased Reusability:
- Specific interfaces can be reused more easily across different parts of the application.
Drawbacks of Adhering to ISP:
- More Interfaces:
- Can lead to an increase in the number of interfaces, making the codebase larger.
- Initial Complexity:
- Designing multiple specific interfaces may require more detailed planning and initial effort.
Mitigating Drawbacks:
- Balanced Approach:
- Apply ISP judiciously, balancing between simplicity and the need for specificity.
- Clear Documentation:
- Maintain clear and concise documentation to help developers understand the purpose of each interface.
- Use of Design Patterns:
- Employ design patterns that naturally adhere to ISP, such as the Adapter pattern, to manage complexity.
Conclusion:
By thoughtfully understanding and applying the Interface Segregation Principle, you can create more maintainable, understandable, and flexible software. Ensuring that clients only depend on the interfaces they use promotes better software design and enhances the overall quality of the codebase.
Liskov Substitution Principle
Dependency Inversion Principle
Check My GitHub Swift Playground Repo.
Top comments (1)
Great write-up! Also wrote some thoughts about it but in context of Go. Although Golang is not a purely object-oriented language, we can still apply SOLID principles to improve our Go code - packagemain.tech/p/mastering-solid...