This is the fourth of five posts talking about the SOLID principles. This principles are a great guide to write good source code. Each post will talk about one of the principles:
- S Single responsability principle
- O Open/Closed principle
- L Liskov substitution principle
- I Interface segregation principle
- D Dependency inversion principle
Interface segregation principle
Its better to have many small interfaces, than a few big ones. Every code must only depend on the interfaces that directly needs and no more.
Sample
Imagine a Plane class that has some functionalities at its service: like flight control and comunications. Its better to segregate those functionalities into two separte interfaces than have only one.
class Plane:
def __init__(self):
self.funcs = PlaneFucntions()
class PlaneFunctions:
def communitation1:
// code
def communitation2:
// code
def control1
// code
It’s better to segreate complex interfaces into smaller ones:
class Plane:
def __init__(self):
self.controls = PlaneControls()
self.comms = PlaneCommunications()
class PlaneControls:
def control1
// code
class PlaneCommunications:
def communitation1:
// code
def communitation2:
// code