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.

  1. i cannot use interface object directly in switch statement. switch statment accepts enum, char, byte, short, int, , string.
  2. 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

  1. can used in switch-case statement, satisfying above 2 issues.
  2. 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

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -