Solid Principles
Solid Principles
What are the SOLID principles?
In software engineering, SOLID is a short form for five object-oriented design principles intended to make software designs more understandable. They are a subset of many principles. it was introduced by Robert C. Martin in his 2000 paper Design Principles and Design Patterns.
These SOLID principles
should be followed when designing software.
Lets
see what are the SOLID principles.
1.
Single
responsibility
2.
Open-close
3.
Liskov
substitution
4.
Interface
segregation
5.
Dependency
inversion
1. Single responsibility
Robert C. Martin describes it as,
“A class should have one and only one reason to
change, meaning that a class should have only one job”
It means that every object in the system should have one
responsibility and all the object code should be focused on achieving this
responsibility.
This principle does not mean that classes should only
have one method or property. Sometimes there can be few methods but only one
responsibility. Also sometimes can be only one method but several responsibilities.
This is not only about a class it also can be used as function,
module, API etc.
Let’s see what the benefits of single
responsibility principle are,
- it’s
easier to be followed, understood, debugged, and refactored.
- You can make changes without any hesitation.
- If something goes wrong it will only affect on one thing not an entire system.
2. Open-close
The Open-Closed
Principle requires that classes should be open for extension but closed to
modification. Modification means changing the code of an existing class, and
extension means adding new functionality to the code.
But how should we able to add new functionality without touching the existing code for the class?
It can be done with the help of abstract classes and interfaces.
Let’s clarify with real world example;
An electrical adopter is
a very good example for open close principle because we all know an adapter in the wall can’t
be modify, which means we cannot change it once it is fitted. But it always
provides a method of extension, so we can plug in an extension board of an
adapter for more adaptation.
Let’s see what the benefits of open/close principle are,
· It reduces
coupling and increased flexibility.
· It is
limiting the need to change of source code.
· Reduced
the risk of introducing new bugs to existing code.
In Liskov substitution
principle parent classes should be easily substituted with their child classes
without blowing up the application. This applies to inheritance hierarchy.
This principle
requires the objects of subclasses to behave in the same way as the objects of
superclass. If a subclass does something that the client of superclass does not
expect, then this is in violate LSP.
Now let’s see what the benefits of Liskov
substitution principle are,
·
It
reduces the complexity and much easier to debugging.
·
Because
of this code become clear.
4. Interface segregation
Robert C. Martin describes it as, “A client should
never be forced to implement an interface that it doesn’t use, or clients
shouldn’t be forced to depend on methods they do not use.”.
In here larger interface should be split into smaller
once by doing so we can ensure that implementing classes only need to be concerned
about the methods that of interest to them.
Let’s see what the benefits of Interface segregation principle
are,
·
It is reducing coupling
·
Smaller interface are easier to
implement
·
Minimized dependencies on used members
5. Dependency inversion
Dependency inversion states that high level modules
should not depend upon low level modules. They should depend on abstraction.
Also this principle suggest abstraction should not
depend on details, details should depend upon abstractions.
If all the details behind those abstraction change
then our class is still safe.
Let’s see what the benefits of
Dependency inversion principle are,
· Make design easier to change
· It is keeps coupling rows.
Comments
Post a Comment