SOLID Principle (Quick Read)

 The famous design principle comprises 5 design strategies that a developer should follow for a successful application development

  1. Single Responsibility Principle
  2. Open/ Close Principle
  3. Liskov Substitution Principle
  4. Interface Segregation Principle
  5. Dependency Inversion

Single Responsibility Principle(SRP)

No code unit(function/class/package) should have more than one responsibility.
 
Bad Ex:
  Class Bird
 { 
   String result;;
   public fly(Bird bird)
   {
    if(bird==piegeon)
      result="flys 20 meter hight";
   else if(bird==hen)
      result="flys 5 meter";
   else
       result="not measured yet";
   }
}

This class should design such a way that it should work well for all the types of birds current design is hectic to maintain for the following reasons
  •  difficult to test.
  • difficult in parallel programming 
  • understanding the code is difficult
solution: Break the code into multiple packages or classes

How to identify SRP is violated(Litmus test to SRP)? 
  • Multiple if-else ->huge chance to violate   SRP principle
  • Monster Method->Doing more than what it states to do( eg: save() method should do only data save, it should not do any connection factory instantiations or other data manipulations)
  • Utility classes-> utility packages are strictly bad don't write junk utility. if need a date functionalities to create Date utility separately.

Open/ Close Principle

Whenever we have to add new features, our existing codebase should not require any modifications. Our code should be open for extensions mostly by adding new classes/methods. which means our code should be open for extensions and closed for modifications.

for example, in the above method, we want to add a new bird parrot then we need to add a new if-else block, to avoid that we can follow the below architecture by creating a Bird class separately and add new Bird varieties by extending the Base Bird class




Liskov Substitution Principle

The object of parent class is completely replaceable by any object of the child class.

For Eg:
 
class Bird{

fly(){
}
makeSound()
{
}
}

class Peacock extends Bird
{
fly(){
}
makeSound()
{
}
}

class Hen extends Peacock
{
fly(){
}
makeSound()
{
}
}

now create an Object like below 
Bird obj=new Bird(); (Bird can replace with Peacock or Hen but the below code will work perfectly)

obj.fly();
obj.makeSound();

Negative scenario

class Ostrich extends Bird
{
fly() 
{
throws new CantFlyException();  // This is against Liskov's principle which gives different implementaion for subclass 
}
makeSound()
{
}
}

But Liskov substitution fails in 2 places 
  1. When a subclass throws an exception.(for this we need a separate try-catch in the calling code like try{ obj.fly()} catch(Exception ex){}
  2. when subclass gives a different meaning to the method of the parent class

Interface Segregation Principle

Interfaces should be designed as lite as possible, Better to focus on a single job. For eg: in java Runnable interface has a single run method. (Like Python Lambda & Java Lambda)

Dependency Inversion

Any 2 concrete class should depend on each other via a layer of abstractions. 

For eg: PayTm have two payment agent like RazorPay & JustPay the implemetations can be done using one PaymentLayerAbstractions.

Interface PaymentInterface{
void pay()
{
}
}
class RazorPay implements PaymentInterface
{
void pay()
{
System.out.println("Payed using Razor pay");
}
}

class JustPay implements PaymentInterface
{
void pay()
{
System.out.println("Payed using JustPay ");
}
}
class PayTm{
PaymentInterface pay;
PaymentInterface(PaymentInterface pay)
{
this.pay=pay
}
public static void main(String args[])
{
PaymentInterface payment= new  JustPay(); // can substitute easily with RazorPay no further cahnges required
payment.pay();
}
}


We injected the dependency using constructor like below
PaymentInterface(PaymentInterface pay)
{
this.pay=pay
}

Spring Boot helps to inject using Annotation based injections also.

Happy reading 😌

Comments

Popular posts from this blog

Linux Basics

JAVA 8 Predefined Functions

RabbitMQ