Tuesday 13 June 2017

Solid Principles In Java


SRP  Principle In Java

This tutorial talks about the list of solid principles in java,what are there benifits,where they are essentials to use. Before we start first we need to know that which are the solid principles in java and what there specifications so lets start.

In java there are five most used solid principles are there.

1.SRP (Single Responsibility Principle) :- A class should have only one responsibilty, nothing more than that for ex. A invoice class should do only one responsibility like calculate tax, total amount to be paid etc, display the things over the user view is another responsibility . So keep one task attached to one class or module.Illustration about this are explain below in further section. 

From Wikipedia: In object-oriented programming, the single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.

From Clean Code : A class or module should have one, and only one reason to change.

To understand the SRP principle first we need to understand the voilation condition of this and then we refactor it according to SRP principle says.So lets move on to the Code Area


Voilating SRP Principle

Invoice Class :- 


public class Invoice {

private int no;
private double cost;
private float discount;
private String name;
static final float vat = (float) 12.5;

public Invoice(int no, double cost, String name, float discount) // Constructor created here
      { 
this.no = no;
this.cost = cost;
this.name = name;
this.discount = discount;
}

public double calculateDiscountCost()                      //1.Calculation Responsibility
       {
double discount = (cost - (cost * this.discount) / 100);
return discount;
}

public double calculateTax() 
       {
return (vat * cost) / 100;
}

public double grandTotakAmount()
        {
return cost + calculateDiscountCost() + calculateTax();
}

public void printInfo()                                              //2. Display Information Responsibilty
 {
System.out.println("Discount on invoice is" + calculateDiscountCost());
System.out.println("Tax on invoice is " + calculateTax());
System.out.println("Grand toral is " + grandTotakAmount());
}

}


Invoice Test Class :- 


public class InvoiceTest {
public static void main(String args[])     //Main Method
{
Invoice invoice=new Invoice(1,50000,"New",50);
invoice.printInfo();
}
}

OUTPUT :-

Discount on invoice is 25000.0
Tax on invoice is 6250.0
Grand toral is 81250.0


So from above information we can clearly say that invoice class has two responsibilities 
1. calculation of discount , tax,total
2.display information

so this condition voilates SRP principle so we need to refactor it according to SRP principle study Says.

Refactoring for Satisfying SRP on Invoice Class 


1. Invoice Class :-

public class Invoice 

private final int no;
private final double cost;
private final float discount;
private final String name;
private static final float vat = (float) 12.5;

public Invoice(int no, double cost, String name, float discount) {
this.no = no;
this.cost = cost;
this.name = name;
this.discount = discount;
}


public double calculateDiscountCost() 
{

double discount = (cost - (cost * this.discount) / 100); // Calculation Responsibilty 
return discount;
}


public double calculateTax() {
return (vat * cost) / 100;
}


public double grandTotakAmount() {
return cost + calculateDiscountCost() + calculateTax();
}


public int getNo() {
return no;
}


public double getCost() {
return cost;
}


public float getDiscount() {
return discount;
}


public String getName() {
return name;
}


public static float getVat() {
return vat;
}
}

Create another class which does the display information of Invoice separately.

2.Class ConsolePrinter :-

public class InvoiceConsolPrinter {
public static void printInfo(Invoice invoice) // Display responsibility
{
System.out.println("Discount on invoice is" + invoice.calculateDiscountCost());
System.out.println("Tax on invoice is " + invoice.calculateTax());
System.out.println("Grand toral is " + invoice.grandTotakAmount());
}

}

3. Test Class :-

public class InvoiceTest {
public static void main(String[] args) {

Invoice invoice = new Invoice(1, 50000, "new", 50);
InvoiceConsolPrinter icp = new InvoiceConsolPrinter();
icp.printInfo(invoice);

}

}

Output :- 

Discount on invoice is25000.0
Tax on invoice is 6250.0
Grand toral is 81250.0


So this is complete details about SRP principle in java with voilation and refactoring example briefly. In next Section We are going to see Another most important principle in java that is OCP (Open Close Principle).


Keep Reading....!!!!!!!!!

No comments:

Post a Comment

Spring-App

Application In Spring Spring Framework is an open source Java Platform that provides comprehensive infrastructure support for developi...