Implement something like an extendible enum in Java -
to attain type substitutability on multiple enums in java can have them implement interface. using object of interface in switch-case challenging.
- i cannot use interface object directly in switch statement. switch statment accepts enum, char, byte, short, int, , string.
- i can switch on
object.getuniqueid()
getuniqueid
member of interface, in case have hard-code values of case statements. imo, ugly , impossible refactor.
what needed implementation
- can used in switch-case statement, satisfying above 2 issues.
- be type substitutable.
any pointer helpful.
you should either use single enum has possible enum
values or need check type before switch.
myinterface value = ... if (value instanceof enymtype1) { switch((enumtype1) value) { case ... } } else if (value instanceof enumtype2) { switch((enumtype2) value) { case ... } }
instead of using switch can have map<myinterface, consumer<myinterface>>
extensible dynamically has close same performance of switch.
Comments
Post a Comment