Interface Segregation Principle

Palanivel SundaraRajan GugaGuruNathan - Jun 10 - - Dev Community

The Interface Segregation Principle (ISP) suggests that a class should not be forced to implement methods it doesn’t need.

Bad Practice - Principle not followed


            +------------------+
            |   <<interface>>  |
            |     IVehicle     |
            +------------------+
            | + drive()        |
            | + fly()          |
            +------------------+    
                ^         ^
                |         |               
+------------------+    +------------------+
|      Car         |    |    Airplane      |
+------------------+    +------------------+
| + drive()        |    | + drive()        |
| + fly()          |    | + fly()          |
+------------------+    +------------------+
Enter fullscreen mode Exit fullscreen mode

Good Practice - Principle followed



+------------------+              +------------------+
|   <<interface>>  |              | <<interface>>    |
|      ICar        |              |    IAirplane     |
+------------------+              +------------------+
| + drive()        |              | + fly()          |
+------------------+              +------------------+
        ^                                ^
        |                                |
+------------------+              +------------------+
|      Car         |              |    Airplane      |
+------------------+              +------------------+
| + drive()        |              | + fly()          |
+------------------+              +------------------+
| <<implements>>   |              | <<implements>>   |
|      ICar        |              |    IAirplane     |
+------------------+              +------------------+

Enter fullscreen mode Exit fullscreen mode
. .