Let’s learn SOLID principles with one big example. This is the most common interview question as well.
First, we need to break down the abbreviation.
S: Single Responsibility Principle
O: Open-Closed Principle
L: Liskov’s Substitution Principle
I: Interface Segregation Principle
D: Dependency Inversion Principle
O: Open-Closed Principle
L: Liskov’s Substitution Principle
I: Interface Segregation Principle
D: Dependency Inversion Principle
Pretty cool, huh? Let’s explore each of them.
Single Responsibility Principle
A class must have a specific responsibility and nothing else. It should change for only one reason.
So the responsibility is actually encapsulated within the class; our class HAS-A Responsibility.

As you can see in this UML: class IPhone has properties that can define an IPhone. But hold on -- what is CalculateTotal_Sale() doing here?
Do you see the problem? It is not the responsibility of an IPhone class to calculate Total sale
Problem
Do you see the problem? It is not the responsibility of an IPhone class to calculate Total sale
Problem
What if tomorrow I have IPad class, I’ll have to re-code the same method for that class as well.
Solution
Solution
Rather we can move the responsibility of calculating Total sale to new class Sale. And IPhone can encapsulate Sale to calculate the total sale.
Here, now Sale is managing the responsibility of calculating TotalSale(). And our IPhone class simply reuse this responsibility rather than creating one by itself. Hence IPhone has the TotalSale().
Open-Closed principle
Open for Extension, Closed for Modification. Software entities (objects, modules, functions) should be open for extension but closed for modification. This simply means that an entity should be easily extendable without modifying them-self.
Problem
Problem
We know that different IPhone models have different specs, right? IPhone XR has different specifications than IPhone X.
Solution
Solution
We must abstract our problem so that our IPhone class can delegate its an implementation based on model type.

Now IPhone can ask for specific Model and set specifications based on the model it gets.
Here class IPhone is closed for modification but it is open for extension. Note that class IPhone now has an IModel encapsulated. So ModelName property is not the responsibility of IPhone class anymore. Yet again we are using our first design principle(Single responsibility). And our abstraction is generic enough to satisfy any model apple ever creates.
Our client, an IPhone, doesn't have to change just because Apple decides to have a new Model (closed for modification). And apple can now have “n” number of models (open for extension).
Liskov’s Substitution Principle
Parent classes should be easily substituted with their child classes.
In our UML above as we can see that, we have created IModel interface, now any class which inherits the IModel must define all of its methods in order to satisfy IS-A relationship else you’ll get a compile-time error. But your compiler is not going to be there when you inherit classes rather than interfaces.
In our UML above as we can see that, we have created IModel interface, now any class which inherits the IModel must define all of its methods in order to satisfy IS-A relationship else you’ll get a compile-time error. But your compiler is not going to be there when you inherit classes rather than interfaces.

IPhone HAS-A CPU class, IPhone could have Bionic chips but none of the IPhone is compatible with Snapdragon.
Problem
Problem
Have you ever seen IPhone with Snapdragon? Hell no!!! Right? So when we try to assemble Snapdragon’s CPU to our IPhone class it will throw an error as you can see in Snapdragon865 class it is throwing a not implemented exception. Because IPhone X & XR are only compatible with Bionic chips.
Solution
Solution







Selvam KPosted May 25, 2020, 12:33 AM
Simply explained to the core. Really appreciate it.
Santhosh PatilPosted May 13, 2020, 3:48 AM
Superb.. ,,,,,,,