SOLID Principles – Which SOLID Principles Are Broken by This Class Diagram?

class-designclass-diagramobject-orientedsoliduml

I just read about all the 5 principles S,O,L,I,D and I like test it in little example if I understand them correct.

Which SOLID principles are hurt by this class diagram?

enter image description here

I think what is broken is Liskov Substitution Principle because if we take upper class Book and replace it with its lower class E-book then application we have here will stop working because there is no way / function to access each other class, both classes here just have their own functions and that's it. For the same reason the principle Dependency Inversion Principle should be hurt by this class diagram as well because there is no object of interface to communicate with the given classes here.

I think the other principles are not hurt.

Best Answer

The only principle which is not respected here is the Interface Segregation Principle: E-book inherits stock and replenishStock() which are absolutely useless for an electronic book.

If you want to have a proper segregation of interfaces, you'd need something like the following, where Book describes a content, and PaperBook and e-Book correspond to sellable items with the generic content:

enter image description here

In your diagram, there is nothing wrong with the Liskov Substitution principle, since an e-Book is a specialisation of a Book, and therefore has the full interface of Book (it's implicit). Of course, stock and replenishStock() don't make a lot of sense, but nothing tells us that the contract is not respected (e.g. the replenishStock could very well set the stock to an arbitrary high value and meet all the preconditions, postconditions and invariants).

Dependency inversion is not relevant here.