Skip to content

Instantly share code, notes, and snippets.

@vipul-zambare006
Created April 21, 2021 16:31
Show Gist options
  • Save vipul-zambare006/6fb42262ca11d41a47061f84e35d44cd to your computer and use it in GitHub Desktop.
Save vipul-zambare006/6fb42262ca11d41a47061f84e35d44cd to your computer and use it in GitHub Desktop.
Solid principles
SOLID principles:
S: Single Responsibility Principle:
1. The class should have only and only one reason to change.
2. Helps in writing maintainable code. If there will be change in requirement you only nee
O: OCP => Open Close Principle:
1. Classes should be open for extension and closed for modification.
2. Eg: Create and abstract class which defines common behaviour and let other developers extend from that class to create their own implementation or extension to existing implementation
L: LSP => Liskov Substitution Principle:
1. Parent classes should be completely substitutable by child classes.
2. Child classes can do all the things that parent class can do
3. Employe is parent class => Manager is extended from Employee => CEO is extended from Employee.
4. Now Manager can do all the methods that Employee class can do.
I => ISP => Interface Segregation Principle:
1. Create smallest possible interfaces so that you do not have to force classes to implement methods declared in interface but are not making any sense to class implementing that interface.
2. You have to add dummy implementation in class who is implementing an interface. Just to keep compiler happy.
3. Clients should not be forced to implement interfaces that they don’t use.
4. Avoid fat interfaces and write small small interfaces.
5. Eg:
interface Animal { void feed() }
interface Pet { void groom() }
class Dog implements Animal, Pet { void feed(); void groom() }
class Tiger implements Animal { void feed() }
In above example Tiger can’t be groomed so its only implementing Animal interface.
If groom method was originally declared in Animal interface then Tiger class has to have some dummy implementation for Groom(). As Tiger can not be groomed. So to avoid this unwanted forcing on classes. we create small small interfaces.
D => DIP => Dependancy Inversion Principle:
Classes should depend on abstraction but not on concretion
This principle suggest that “classes should depend on abstraction but not on concretion”.
What does it mean that we should be having object of interface which helps us to communicate with the concrete classes.
What do we gain from this is, we hide the actual implementation of class A from the class B.
So if class A changes the class B doesn’t need to care or know about the changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment