3

Say, I have some classes:

class NPC {
    Attributes attributes;
    Skills skills;

    void doStuff() { //doStuffff }
}

class Player {
    Attributes attributes;
    Skills skills;

    void doStuff() { //doStuffff }
}

And an Enum for these classes:

enum Attributes {
STRENGTH, INTELLIGENCE, AGILITY...//etc
}

enum Skills {
GUNS, MELEE, ATHLETICS...//etc
}

Now, let's say I wanted to make it so that each instance of either class Player or NPC could actually have their own separate instance of Attributes or Skills which could add and subtract values of the enums themselves. Is this possible? If not, would it be possible to use methods within an innter nested enum to manipulate the field values of a class it resides in?

This may sound somewhat insane, ridiculous, and just down right premadonnic, but when you have to find the right design solution...well, sometimes you just have to think outside the box.

Edit:

I found an interesting solution, though I doubt it's as good as the map idea.

I figured it'd just be interesting to post :D

public final class StatControl {

    int agility;
    int endurance;
    int intelligence;
    int intuition;
    int luck;
    int speed;
    int strength;

}

enum Attributes {
    AGILITY {
        void add(int value) {
            test.agility += value;
        }
    },
    ENDURANCE {},
    INTELLIGENCE {},
    INTUITION {},
    LUCK {},
    SPEED {},
    STRENGTH {};

    abstract void add(int value);

    StatControl test;

    void receiveObj(StatControl test) {
        this.test = test;
    }

}

As you can see, a class holds an integer representation of the enums, but the enum itself acts as a control center which can manipulate whatever instance of the StatControl object passed to it.

Maps are probably better though - I could see where this could easily get messy.

zeboidlund
  • 9,731
  • 31
  • 118
  • 180

3 Answers3

3

On further consideration, I think that perhaps the answer to your problem is to use the same enums for all but different EnumMaps for your different classes if you want each enum to have a different value of some sort associated with it. If this answer is way out in left field, sorry, but then please clarify your question. :)

For more on EnumMaps, please look here:
Taming Tiger: Beyond the basics of enumerated types
EnumMap API

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Not at all. That seems like a good idea...I'll post another thought I had to just for some clarification, to see what you all think. – zeboidlund Jun 24 '11 at 16:29
1

You can use EnumMaps like

abstract class Actor {
    final Map<Attribute, Integer> attributes = new EnumMap<Attribute, Integer>();
    final Map<Skill, Integer> skills = new EnumMap<Attribute, Integer>();

    abstract void doStuff();
}

class NPC extends Actor {
    void doStuff() { }
}

class Player extends Actor {
    void doStuff() { }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

You can add attributes to every instance of an Enum. See the Planets example:

http://download.oracle.com/javase/1,5.0/docs/guide/language/enums.html

leifg
  • 8,668
  • 13
  • 53
  • 79
  • By instance, do you mean separate instances of the same enum type? – zeboidlund Jun 24 '11 at 16:40
  • yes, see the different planet instances (earth, mercury, venus etc..) in the example – leifg Jun 24 '11 at 17:18
  • but this question is asking the equivalent of "Can I have X instances of `MARS` each with a different radius?" such as having the STRENGTH Attribute for 9 people having 9 different values. – Stephen P Jun 24 '11 at 19:07