Given a finite number of elements that differ in nature, is it better to represent them with complex enum enums and constructors, or subclass them? Or is there a better approach?
To give you some context, in my small RPG program (which, ironically, should be simple), the character has different items in his inventory. Elements vary depending on their type and use and effect.
For example, one inventory item is a spell scroll called Gremlin, which adjusts the Utility attribute. Another item may be a sword called Mort, which is used in battle and deals damage.
In my RPG code, I now tried two ways to represent inventory items. One of the methods was a subclass (for example, InventoryItem β Spell β AdjustingAttributes; InventoryItem β Weapon β Sword) and, if necessary, instantiated each subclass and assigned values ββsuch as the Gremlin and Mort names.
Another way was to add enumerations and enumeration constructors. For example, I created enumerations for itemCategory and itemSpellTypes and itemWeaponTypes, and the InventoryItem enumeration was as follows:
public enum InventoryItem { GREMLIN(itemType.SPELL, itemSpellTypes.ATTRIBUTE, Attribute.UTILITY), MORT(itemType.WEAPON, itemWeaponTypes.SWORD, 30); InventoryItem(itemType typeOfItem, itemSpellTypes spellType, Attribute attAdjusted) {
Is there a better Java programming practice than these two approaches? Or, if this is the only way, which of the two is better? Thanks in advance for your suggestions.
java enums subclassing
Arvanem
source share