0.00 solid principles - amresh087/Question GitHub Wiki
Why solid principles ?
To create understandable, readable, and testable code that many can work together
S.O.L.I.D Principles
-
Single Responsible Principle
A class should have one, and only one, reason to change. This means that a class should focus on a single responsibility or functionality. When a class does too much, it becomes difficult to maintain because changes in one area can inadvertently affect others.
-
Open-closed Principle
A classes should be open for extension but closed for modification. In practice, this means you should be able to add new behavior without changing existing code. This is usually achieved through abstraction (like using interfaces or abstract classes) and polymorphism..
-
Liskov Substitution
Subclasses should be replaceable with their base classes without affecting the correctness of the program. That means any instance of a parent class should be able to be substituted by an instance of a child class without unexpected behavior.
-
Interface Segregation
classes should not be force to implement a method they do not need.
Suppose you have interface called shape. In this interface having a method called getArea() then we can implement this interface Rectangle, square, circle. If I want to implement in Qube class then it is not good because in Qube no need
So we need create Segregation interface like below
interface Shape{
//defined some some method that can use in 2DShape and 3DShape
}
interface 2DShape extends Shape{
//defined some some method that can use in 2DShape
}
interface 3DShape extends Shape{
//defined some some method that can use in 3DShape
}
-
Dependency Inversion
High level classes should not depend on low level classes. Both should depend upon abstractions.
Suppose you have a class called Printer with print method. But in future we need implement CsvPrinter then we need to create interface and that interface used in both classes. where we need call then create indirect object