Definition -
The use of an interpreter that used to interpret the requests from the client through the use of representations.
Scenario -
Let us take an example of currency conversion where in a client wants to convert money from USD to either INR or Euros. The system uses the conversion factor to calculate the amount and send it back to the client.
Based on the above scenario -
1) An interpreter called currencyconverter.
2) A class which will have the actual interpretation called convertor.
3) A class called USDtoINR and another called USDtoEur
usdtoeurjava
usdtoinr.java
4) A client which will use the interpreter to covert the value.
Place to use -
1) It can be used in places where you want one format to be converted into another format.
2) It can be used in places where you are checking for grammar or evaluating an expression written on an UI.
To be careful about -
1) Performance of the code can be an issue, so you need to use this pattern very carefully.
The link for the code is present in the link here
The use of an interpreter that used to interpret the requests from the client through the use of representations.
Scenario -
Let us take an example of currency conversion where in a client wants to convert money from USD to either INR or Euros. The system uses the conversion factor to calculate the amount and send it back to the client.
Based on the above scenario -
1) An interpreter called currencyconverter.
package InterpretorPattern;
public class currencyconvertor {
double cusdtoinr=60.0;
double cusdtoeur=1.3;
public double getusdtoinr(double sourceamount) {
return sourceamount*cusdtoinr;
}
public double getusdtoeur(double sourceamount) {
return sourceamount * cusdtoeur;
}
}
2) A class which will have the actual interpretation called convertor.
<pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>package InterpretorPattern;
public interface convertor {
double convert(currencyconvertor cc);
}
</code></pre>
3) A class called USDtoINR and another called USDtoEur
usdtoeurjava
package InterpretorPattern;
public class usdtoeur implements convertor {
private double amt;
public usdtoeur(double curr) {
this.amt=curr;
}
@Override
public double convert(currencyconvertor cc) {
return cc.getusdtoeur(amt);
}
}
usdtoinr.java
package InterpretorPattern;
public class usdtoinr implements convertor{
private double amt;
public usdtoinr(double curr) {
this.amt=curr;
}
@Override
public double convert(currencyconvertor cc) {
return cc.getusdtoinr(amt);
}
}
4) A client which will use the interpreter to covert the value.
package InterpretorPattern;
public class interpretordemo {
public static void main(String args[]) {
System.out.println("Converting $100 to EUR");
double amt=100;
double convertedamt=0;
currencyconvertor cc=new currencyconvertor();
String converttocurr="EUR";
convertor c=null;
if(converttocurr.equalsIgnoreCase("INR")) {
c=new usdtoinr(amt);
convertedamt=c.convert(cc);
System.out.println("The converted amount in INR is: "+convertedamt);
} else {
c=new usdtoeur(amt);
convertedamt=c.convert(cc);
System.out.println("The converted amount in EUR is: "+convertedamt);
}
}
}
Place to use -
1) It can be used in places where you want one format to be converted into another format.
2) It can be used in places where you are checking for grammar or evaluating an expression written on an UI.
To be careful about -
1) Performance of the code can be an issue, so you need to use this pattern very carefully.
The link for the code is present in the link here
No comments:
Post a Comment