Java 8 has introduced functional programming.Before java 8 there is nothing we can not not do with java7,but with functional programming we can write better code ,more readable and maintanable code.Lets look at what kind of problems functional programming solves.
In object Oriented programming everything is class and object ,we can not have any code in isolation.Each and every line of code must belong to class.This can be a problem sometime, not always this is problem but there are some situations where we need some block of code to pass as a method argument or we need to return some block of code from method which is not possible with pure object oriented programming.In order to do that we used to pass an object which has that block of code instead of that block of code.
We use inline values in our day to day activities like,
int a = 10;
double pi = 3.14;
In functional programming we are assignning code to a variable like
aBlockOfCode = {
/*
*code goes here
*/
}
Lets see an example of EMI calculator.We have base interface as EMIcalculator and its concrete implementation as HomeLoanEMICalculator.
EMICalculator interface :
Concrete HomeLoanEMICalculator class:
Client Programme:
This is the pure Object oriented Style of writing code.Here we need pass just behaviour of calculateEMI method but for that we have created HomeLoanEmiCalculator object and pass that object to processRequest() method.See below image,
Instead in functional style programming just pass block of code as a argument to processRequest method() like below,i have copied the method signature as it is in blow image. We just need to remove acccess specifier as values do not have access specifiers and remove method name and return type as java compiler is smart enough to identify return type.After doing changes it looks like below,
See instead of passing HomeLoanEMICalculator's object who has calculateEMI() behaviour here i am directly passing passing behaviour.So instead of passing thing which has behaviour pass the behaviour itself is what functional programing does.
In object Oriented programming everything is class and object ,we can not have any code in isolation.Each and every line of code must belong to class.This can be a problem sometime, not always this is problem but there are some situations where we need some block of code to pass as a method argument or we need to return some block of code from method which is not possible with pure object oriented programming.In order to do that we used to pass an object which has that block of code instead of that block of code.
We use inline values in our day to day activities like,
int a = 10;
double pi = 3.14;
In functional programming we are assignning code to a variable like
aBlockOfCode = {
/*
*code goes here
*/
}
Lets see an example of EMI calculator.We have base interface as EMIcalculator and its concrete implementation as HomeLoanEMICalculator.
EMICalculator interface :
public interface EMICalculator { public double calculateEMI(double principalAmount,int noOfMonths); }
Concrete HomeLoanEMICalculator class:
public class HomeLoanEMICalculator implements EMICalculator { @Override public double calculateEMI(double principalAmount, int noOfMonths) { return (principalAmount*9*Math.pow(1+9,noOfMonths))/(Math.pow(1+9,noOfMonths)-1); } }
Client Programme:
public class Client { public double processrequest(EMICalculator emiCalculator,double principalAmount,int noOfMonths){ return emiCalculator.calculateEMI(principalAmount, noOfMonths); } public static void main(String[] args) { Client client = new Client(); EMICalculator emiCalculator = new HomeLoanEMICalculator(); client.processrequest(emiCalculator, 500000, 12); } }
This is the pure Object oriented Style of writing code.Here we need pass just behaviour of calculateEMI method but for that we have created HomeLoanEmiCalculator object and pass that object to processRequest() method.See below image,
Instead in functional style programming just pass block of code as a argument to processRequest method() like below,i have copied the method signature as it is in blow image. We just need to remove acccess specifier as values do not have access specifiers and remove method name and return type as java compiler is smart enough to identify return type.After doing changes it looks like below,
See instead of passing HomeLoanEMICalculator's object who has calculateEMI() behaviour here i am directly passing passing behaviour.So instead of passing thing which has behaviour pass the behaviour itself is what functional programing does.
Comments
Post a Comment