java - Different Enum method parameters -
i have come across case need make design decision (not program itself, learning others in situation, best @ though going api). have enum class:
public enum type {      captain(2),  // use calcmorale never other     leader(5),   // use calcmorale never other     partier(80); // use calcmorale2 never other      int calcmorale(int inmorale)     {         return inmorale * (100 + morale) / 100;     }      int calcmorale2(int inmorale, int teamcount)     {         return inmorale + morale * teamcount;     }      int morale;      private type(int amor)     {         morale = amor;     }  }   there class character holds 1 of these enums , 1 passing in parameters.
the problem commented, want enums use methods inside enum class. leader adds 5 percent team morale, partier adds 80 each member inside party requiring parameter. solve problem few ways, know general consensus of community on how they'd go it.
i could:
make programmers responsibility call right method. (this sounds wrong me).
give each enum it's own version of function. (this make massive file, , repeating lot of code).
leave responsibility 'character' class has method switch processing this. (this means programmer change calculations , not intended). cause less problems if wanted move enums file later instead. question if breaking encapsulation if should solely enums responsibility.
make type class , define methods via anonymous inner classes. in case, classes added map, opposed having getbyname() in enum class.
--
what consider appropriate? (i prefer classes self dependent possible though using api (it won't be), , how/would suggestion affected depending on amount of enums? , case wanted enum able use both methods? language java.
like said, have think how application going handle this. if want enum class handle itself, need sort of conditional , form of overloading (which means developers responsible calling right methods anyway.
however force developers send teamcount regardless:
public enum type {     captain(2),     leader(5),     partier(80);      private final int morale;      private type(int m){         morale = m;     }      public int getmorale(int inmorale) {          return getmorale (inmorale, 0);     }      public int getmorale(int inmorale, int teamcount) {         switch (this) {             case captain:                    case leader:                 return inmorale * (100 + morale) / 100;             case partier:                 return inmorale + morale * teamcount;         }         return 0;     } }   edit: changed switch 'this'
Comments
Post a Comment