Skip to content

0.00 solid principles

Amresh Verma edited this page Feb 13, 2024 · 12 revisions

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 do one thing therefore it should have only a single reason to change

Sample code

public class Circle
 {
     private double radius;
     public Circle(double radius){
       this.radius=radius;
    }
}

class Program
{
  public static void main(String args[]){
    Circle circle=new Circle(5);
    calculateArea(circle);
    print(circle);
  }

 private static void calculateArea(Circle circle){
   double area=3.14*circle.radius*circle.radius;
   System.out.println(area);
 }

 private static void print(String data){
    System.out.println(data);
 }

} 

Above we can see that is not satisfying Single Responsible Principle. calculateArea method is calculating area of the circle. That method should move to circle class. Same like print method is printing the data on console. So you can use this method some another class. So, we need to create that method in some another class then it will satisfying Single Responsible Principle.

  • Open-closed Principle

A class should open for extension but close to modification.

If you want add some another shape called Rectangle then no need to add method in same class. So better approach create interface then implements two separate classes. It is called Open-closed Principle

  • Liskov Substitution

Drived or child class must be substitutable for their base or parent classes

For example if you have created interface called shape having two implementation class called circle and Rectangle but if you want create class Square then you can extends with Rectangle class but you can notice Rectangle class not fulfill mean in rectangle length and width property present but that property is not any use in square class. So Substitution is not correct then we need to implements shape interface in square class

Suppose you have a situation come if you child class is unable to implement same to same then basically child class is not replaceable to parent class then you need to change this child parent combination then go for new implementation for interface

  • 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