I have different types of classes, and depending on some conditions, I want to delegate the corresponding service , which can handle these types of classes.
Example: I have several classes as follows.
class Student; class Prof; ...
For each class there is a service that implements:
interface IPersonService { void run(); }
And I have mode , which is determined by some conditions:
enum PersonType { STUDENT, PROF; }
When I delegate:
@Autowired private StudentService studentService; @Autowired private ProfService profService; //@param mode assume known public void delegate(PersonType mode) { //assume there are several of those switch statements in my business code switch (mode) { case STUDENT: studentService.run(); break; case PROF: profService.run(); break; default: break; } }
Problem . When introducing additional classes, I need to change both PersonType and add an additional enumeration (this is not a problem), but I also need to extend any switch and add calls to additional delegation services. In addition, I must explicitly transfer these services to the delegate list.
Question : how can I optimize this code, just introduce new services for any additional class and not touch any of the switch statements?
source share