I have a program that handle the math operator(+,*,-,/).I have an interface Operator that have a calculate method like so:
public interface Operator
{
double calculate(double firstNumber,double secondNumber);
}
I have four class (Plus,Minus,Divide,Multiply) that implement the Operator interface like so :
public class Plus implements Operator
{
public double calculate(double firstNumber,double secondNumber)
{
return firstNumber + secondNumber;
}
}
public class Minus implements Operator
{
public double calculate(double firstNumber,double secondNumber)
{
return firstNumber - secondNumber;
}
}
And so on...
I use Map to handle the operator :
static Map<String,Operator> operatorMap = new HashMap<String,Operator>();
static
{
operatorMap.put("+", new Plus());
operatorMap.put("-", new Minus());
operatorMap.put("*", new Multiply());
operatorMap.put("/", new Divide());
}
double output = operatorMap.get(op).calculate(firstNumber,secondNumber);
I should change the program like so:
I have a folder(myfolder).Every one can get the interface and implement own operator and put .class file in myfolder.My program should also work.It means dependency to (+,*,-,/) should remove from my program. For Example some one get the Operator interface and implement % and put .class file in my folder my program still should work(Map implementation must change) Can anyone help me? I am so appreciate.