Skip to main content

Command Palette

Search for a command to run...

Interface Segregation Principle

Published
•1 min read•View as Markdown
P

Hi , thanks for stopping by!

I am full-stack developer and I'm passionate about making complex concepts clear through code and visuals. After all, a picture (and a code snippet) is worth a thousand words!

I hope you enjoy my blog.

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()          |
+------------------+    +------------------+

Good Practice - Principle followed



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