I am new to JEE and wanted to write a basic calculator app, and I am wondering how should I instantiate map with operations. I use Command design pattern for calculator operations with Operation interface that has just one declared public method, int performOperation(int a, int b)
My calc util class, fillMap() adds various operations to the map.
public class CalculatorUtil {
private static Map<String, Operation> operationByStringSymbol;
public static synchronized Map<String, Operation> getMap() {
if(operationByStringSymbol == null) {
operationByStringSymbol = new HashMap<String, Operation>();
fillMap();
}
return operationByStringSymbol;
}
...
and the bean:
@Stateless
public class CalculatorBean{
public int calculate(int firstNumber, String operation, int secondNumber) {
Map<String, Operation> operationByStringSymbol = CalculatorUtil.getMap();
return operationByStringSymbol.get(operation).performOperation(firstNumber, secondNumber);
}
...
Is it an ok solution, or is there a better one? I didn't want to use a singleton bean because in case operations are a lot longer one client could block others for some time, and in this case only the getMap() method is synchronized.
Aucun commentaire:
Enregistrer un commentaire