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.

3
  • 2
    What do you mean by "which could add and subtract values of the enums"? Commented Jun 24, 2011 at 16:16
  • And you could have similar but separate enums by changing the enum name or namespace -- i.e., place them in separate packages, but I wonder if you really want to use non-enum classes here. Commented Jun 24, 2011 at 16:16
  • Another option to consider is to have them use the same enums but different enum maps where you map different values of whatever sort desired to the enums. Commented Jun 24, 2011 at 16:18

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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() { }
}

Comments

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

3 Comments

By instance, do you mean separate instances of the same enum type?
yes, see the different planet instances (earth, mercury, venus etc..) in the example
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.