Search code examples
solid-principlesinterface-segregation-principle

Understanding the motivational poster for the Interface Segregation Principle


Why does the `"motivational poster" for the Interface Segregation Principle on this page say "You want me to plug this in, where?"

enter image description here

The Interface Segregation Principle says

Clients should not be forced to depend upon interfaces that they don't use.

I am not sure how the image and the tagline connect to this principle. It seems to be more of a motto --- albeit vaguely --- for the dependency inversion principle where high level objects should not depend on low level implementations (the plugpoint does not need to know the details of the device being plugged in)

I am aware that these sayings are tongue in cheek, but given how cutely the other saying illustrate the principles, I don't want to misunderstand this particular poster for the ISP.


Solution

  • If important process A has a reference to not so important process B, you have now made process A dependent upon process B even though process A doesn't really need process B for anything.

    "Clients should not be forced to depend upon interfaces they don't use" should read - Clients, classes, modules, systems, people should not know about, have references to, have dependencies on other things that are not needed to meet their functional requirement.

    So let's say that you have a high level important thing like a switch/button to launch some nukes. If you plug a coffeemaker into it, then you run the chance of blowing everything up because you made your important missile launcher depend upon whether your coffeemaker is working properly.

    Here's a real life example:

    A Web client communicates with a web server. The web server communicates with the database.

    If the web client were able to access the database directly, then what would happen if the client goes haywire and sends 1000 requests per second?

    The web server in this case acts as the Interface by mediating the communication between the client and the database. Both the client and the database depend upon, know about the web server but not each other. Because the client doesn't know about(is not plugged into) the database, the database can't be screwed over by the client. Why should the database be dependent upon the client? It works just fine on its own.

    In the poster, that button looks pretty important. So as a SOLID programmer, you need to say NO when someone asks you to plug a coffeemaker or anything that is not required into a missile launcher.