In the below example Enums do the amount of processing that a class would do.
enum TriggerHandlerType {
DASHBOARD {
@Override
TriggerHandler create() {
return new DashboardTriggerHandler();
}
},
COMPONENT_HANDLER {
//...
};
abstract TriggerHandler create();
}
private static TriggerContext getTriggerContext(TriggerHandlerType triggerHandlerType) throws TriggerHandlerException {
return new TriggerContext(triggerHandlerType.create());
}
Enums are usually used for type safe storage of constants where as in this case they will be returning varying values based on the processing logic. In a way its seems to be a comprehensive technique as the Enums here do the state determination themselves which eases the processing of classes. Also since the return values are a subset of finite values, it seems to make some sense to have the processing handled by the Enums themselves.
I do see problem here where this will break the Open-Close principle in SOLID and the class will have increment in lines of code whenever more enums get added, Could anyone share your thoughts on this?