Saturday, 23 April 2016

What are SOLID principles ?


Single responsibility
A class should have only a single responsibility.

Open/closed principle
Software entities like, classes should be open for extension, but closed for modification.

Liskov substitution
Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.

Interface segregation
Many client-specific interfaces are better than one general-purpose interface.

Dependency inversion
One should "Depend upon Abstractions. Do not depend upon concretions"
 
---------------------------------------------------------------------------------------------------------------------

S : Write code to fetch some data from DB with good logs created.
You can create different classes instead of writing all the code in one place :
class 1. For creating DB coonection and closing it
class 2. For fetching data from DB
class 3. Logging (specific module)

O : Write code to choose the increment percentage based on user type.
Instead of writing conditions and logic based on values at one place, you can implement specific logic in different classes with different user types.
Class. User has different logic sections according to different user types
Class 1. Partner has logic section 1
Class 2. Customer has logic section 2
Use factory pattern to call specific logic and method

L : Write classes which can be substitute with a generic type.
List <- ArrayList and List <- LinkedList implements this rule.

List <- ArrayList and List <- UnModifiableList violates this rule. (As per need)


I : Segregate the interfaces with different definitions, so that the functions should not be required to be overridden with useless or null implementation.
instead of having single class :
Shape with methods : getArea(), getVolume(), getPerimeter()
Use :
Class base : Shape with getArea()
Class 1. Shape2D extends Shape with getPerimeter()
Class 2. Shape3D extends Shape with getVolume()

D : IoC is a concept and DI is the implementation method.
Define the class and its dependency on other classes.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.