Most Java EE guidelines suggest that we should define Abstract EJB services (APIs) and then define concrete implementations in order to conform with OO design principles.
But at the injection point, either the EJB is Local or Remote, we are forced to provide the concrete implementation class.
Eg:
@EJB(jndi="java:global/moduleName/ConcreteClassName:fullyQualifiedInterfaceName")
ServiceI service;
Therefore, the question: How on earth does this injection maintains Inversion of Control and one step further: How this may be polymorphic?
EDIT:
Having realized that the question might not be clear, I will try to give an example.
Let us have a system that must calculate salaries for employees. Two salary types exist for now, by it is highly possible that a third will come:
public class Employee{
String name;
SalaryCalcService salaryService;
}
public interface SalaryCalcService {
public double abstract calculateSalary();
}
public class PerMonthCalc implements SalaryCalcService {
public double calculateSalary(){
//return salary with per month calculations
}
}
public class PerHourCalc implements SalaryCalcService {
public double calculateSalary(){
//return salary with per hour calculations
}
}
Somewhere in the system concrete objects are created:
SalaryCalcService perMonth = new PerMonthCalc();
SalaryCalcService perHour = new PerHourCalc();
During system everyday work, new employees are created:
Employee emp1 = new Employee();
emp1.serSalaryService(perMonth);
//...
And finally, client code needs to use all these entities and run a calculation for payments:
for(Employee emp : employees){
double salary = emp.getSalaryService().calculateSalary();
}
In the client code, i don't have to know, neither care about what's the specific implementation of SalaryCalcService is.
More importantly, if a third salary calculation method will come. I will just create a third SalaryCalcService subclass and modify the point where the Concrete objects are created. NO CLIENT CODE WILL BE MODIFIED.
How can i implement this on an Java EE environment, where the service performs DB and other transactions and has to be an EJB?