Overview Of S.O.L.I.D
While Designing a class the principles of S.O.L.I.D are guidelines that can be
applied to remove code smells.
S.O.L.I.D
-
Single Responsibility Principle (SRP)
-
Open/Close Principle (OCP)
-
Liskov Substitution Principle (LSP)
-
Interface Segregation Principle (ISP)
-
Dependency Inversion Principle (DIP)
Single Responsibility Principle (SRP)
It states that every object should have a single responsibility, and that
responsibility should be entirely encapsulated by the class. There should not be
more than one reason to change the class. This means class should be designed
for one purpose only. This principle states that if we have 2 reasons to change
for a class, we have to split the functionality in two classes. Each class will
handle only one responsibility and on future if we need to make one change we
are going to make it in the class which handle it. When we need to make a change
in a class having more responsibilities the change might affect the other
functionality of the classes.
Example- Employee class: This class is using for CRUD functionality of Employee,
Then it should not have any method to MAP Employee attributes with Databse
columns, Because later if there is any new column added in Databse, the class
need to be modified which is violationg the rule of Single Responsibility
Principle.
Open/Close Principle (OCP)
It states that Class should be open for extension not for modification. Usually,
many changes are involved when a new functionality is added to an application.
Those changes in the existing code should be minimized, since it's assumed that
the existing code is already unit tested and changes in already written code
might affect the existing functionality. This is valuable for Production
environment where the source codes has already been reviewed and tested. Adding
the New functionality may causes the problem on existing code.
Liskov Substitution Principle (LSP)
Basically when we designed a class, we use maintain Class
hierarchies. We must make sure that the new derived classes just extend without
replacing the functionality of old classes. Otherwise the new classes can
produce undesired effects when they are used in existing program modules.
LSV states that if the module using any base class, then the
reference to the Base class can be replaced with a Derived class without
affecting the functionality of the program module.
Example : - A typical example that violates LSP is a Square class that derives
from a Rectangle class, assuming getter and setter methods exist for both width
and height. The Square class always assumes that the width is equal with the
height. If a Square object is used in a context where a Rectangle is expected,
unexpected behavior may occur because the dimensions of a Square cannot (or
rather should not) be modified independently. This problem cannot be easily
fixed: if we can modify the setter methods in the Square class so that they
preserve the Square invariant (i.e. keep the dimensions equal), then these
methods will weaken (violate) the postconditions for the Rectangle setters,
which state that dimensions can be modified independently. If Square and
Rectangle had only getter methods (i.e. they were immutable objects), then no
violation of LSP could occur.
Interface Segregation Principle (ISP)
It states avoid tying a client class to a big interface if only a
subset of this interface is really needed. Many times you see an interface which
has lots of methods. This is a bad design choice since probably a class
implementing. . This can make it harder to understand the purpose of a
component, but it can also cause increase coupling, where by components that
make use of such a component are exposed to more of that components capabilities
that are appropriate.
The Interface Segregation Principle (or ISP) aims to tackle this
problem by breaking a components interface into functionally separate
sub-interfaces. Although a component may still end up with the same set of
public members, those members will be separated into separate interfaces such
that a calling component can operate on the component by referring only to the
interface that concerns the calling component.
Dependency Inversion Principle (DIP)
In an application we have low level classes which implement basic
and primary operations and high level classes which encapsulate complex logic
and rely on the low level classes. A natural way of implementing such structures
would be to write low level classes and once we have them to write the complex
high level classes. Since the high level classes are defined in terms of others
this seems the logical way to do it. But this is not a flexible design.
High-level modules
should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on
details. Details should depend on abstractions.
Khaja MoizuddinPosted Mar 2, 2019, 3:20 AM
If we have sample program for reference it would be very helpful.
knightsPosted Aug 25, 2011, 10:09 AM
Hey Biswa, Did you copy the contents from --------------http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)------------- If such is the case, have you included any reference details about the original content, if i am over looking ? Thanks
Balakrushna SwainPosted Aug 25, 2011, 10:08 AM
OOPS developer should know about these basics of OOD.