SOLID Principles in a glance
3 min read
S stands for Single Responsibility.
A Class should be responsible only for handling any one kind of responsibility.Unnecessary code should be moved to another Class(if necessary).
If you are making changes to the same Class for different type of requirements you are doing it wrong.
O stands for Open/Closed Principle
Code should be open for extension and closed for modification.
You should be able to modify the functionality with out touching the old source code,which can be achieved by making Code extendable.
Consider typical example of calculating area of different shapes. Implementation based on this rule would be maintaining a interface with all contracts and implementing interface for each type of shape.This way we will add a new Class for each shape but need not touch the existing code.
L stands for Liskov Substitution
Every Child Class must be substitutable in the place of Parent Class.
Imagine Class Square & Rect are sub Classes of Shape.Now I should be able to use Square where ever Rect is used.We should make sure return types should be same else you will end up breaking the code.
I stands for Interface Segregation A User should not be forced to implement an interface that it doesn't use.
Don’t keep all methods in one big fat interface.Break them into multiple interfaces,Its no problem to have a interface with only one method.If you have a fat interface you might be breaking singleton principle.
D stands for Dependency Inversion
High and low level modules should not depend on each other instead depend on abstractions.
Simply saying High level Class like SavingDataClass shouldn’t have knowledge of which type of DB is used.It should only know that it can call abstracted save method.