Wednesday 14 June 2017

Solid Principle In Java

OCP Principle In Java

One of the most important principle used in java that is OCP (Open Close Principle) . OCP principle states that the design and writing of code in such way that your code module and classes are always open for extension and closed for modification .Make sure that your existing code should be unchanged.If you want to make some changes in code then class should be extend before modification.


From Wikipedia :- In object-oriented programming, the open/closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification" that is, such an entity can allow its behaviour to be extended without modifying its source code.

So we can say that ocp principle suggest that  code,module ready for extension but close for modification.To understand the ocp principle first we need to understand the voilation condition of this and then we refactor it according to ocpprinciple says.So lets move on to the Code Area.


Voilating OCP Principle

1. Enum Class :-


public enum FestivalType {

NEWYEAR , HOLI, NORAML;

}

2.Fix Deposite Class :- 

public class FixedDeposit  
{
private final int accNo;
private final String name;
private final double principle;
private final int duration;
private FestivalType festival;
private double rate;

public FixedDeposit(int accNo, String name, double principle, int duration, FestivalType festival) 
{
this.accNo = accNo;
this.name = name;
this.principle = principle;
this.duration = duration;
this.festival = festival;
}

public int getAccNo() {
return accNo;
}

public String getName() {
return name;
}

public double getPrinciple() {
return principle;
}

public int getDuration() {
return duration;
}

public FestivalType getFestival() {
return festival;
}

public double calculateInteresrate() {
if (festival == FestivalType.HOLI) {
rate = 12;
} else if (festival == FestivalType.NORAML) {

rate = 8;
} else {
rate = 10;
}
return (principle * duration * rate) / 100;

}

}

3.Test Class :- 


public class FixedDepositTest 

{
public static void main(String[] args)
 {
FixedDeposit fd = new FixedDeposit(101, "User1", 5000, 2, FestivalType.HOLI);
System.out.println("Intrest rate is "+fd.calculateInteresrate());

}
}

4.OUTPUT :-

Intrest rate is 1200.0

So in this example  class has three  festival but if  we want to add  another festival then, need to make modification in same class and that voilates ocp principle. So below we can refactor it by making another festival class. Example are given below. 

5. Interface :- 


public interface IRateStratergy {

public double getRate();

}

6.Fixed Diposite Class :- 


public class FixedDeposit 
{
private final int accNo;
private final String name;
private final double principle;
private final int duration;
private IRateStratergy festival;
private double rate;

public FixedDeposit(int accNo, String name, double principle, int duration, IRateStratergy festival)
        {
this.accNo = accNo;
this.name = name;
this.principle = principle;
this.duration = duration;
this.festival = festival;
}

public int getAccNo() {
return accNo;
}

public String getName() {
return name;
}

public double getPrinciple() {
return principle;
}

public int getDuration() {
return duration;
}

public IRateStratergy getFestival() {
return festival;
}

public double calculateInterestRate() {
return (principle * duration * festival.getRate()) / 100;
}

}


7. NewYearRate Class :-



public class NewYearRate implements IRateStratergy {


@Override
public double getRate() {
return 10;
}

}

8.NormalRate Class :-


public class NormalRate implements IRateStratergy {


@Override
public double getRate() {
// TODO Auto-generated method stub
return 12;
}

}

9. Test Class :- 



public class FixedDepositTest {


public static void main(String[] args) {
FixedDeposit fd = new FixedDeposit(101, "San", 10000, 2, new NormalRate());
System.out.println(fd.calculateInterestRate());
}
}

10.Output :- 

Intrest rate is 1200.0


So this is complete details about OCP 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 Dip(Dependency InversionPrinciple).


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...