Marty
Marty

Reputation: 39456

AS3 Curious structure question

I'm (I think) quite good at structuring my application classes and packages, but lately I've been weighing something up that I could use some advice on:

Classes that contain only static constants, like this:

package //?
{
    public class Elements
    {
        public static const FIRE:String = "fire";
        public static const WATER:String = "water";
    }
}

Which are obviously used in situations like this:

var myElement:String = Elements.FIRE;

I've always placed these classes in packages that contain other classes which make the most use of them. For example, this class might be in game.mobiles because this package contains classes for mobiles (Player, enemies, etc) which make the most use of Elements; they have resistances and damage that can be elemental.

This seems odd to me though, because Elements really doesn't have anything to do with Mobiles (as it's not a Mobile or technically even related to a Mobile).

I've started to wonder if I should put all of my classes like the above into a generic package such as game.statics, though this seems just as messy from some points of view.

What should I look at doing here to have an as clean and understandable structure possible when it comes to these little classes?

Upvotes: 0

Views: 112

Answers (2)

Joshua Sullivan
Joshua Sullivan

Reputation: 1139

I guess if I have a lot of constants that are pertinent to a particular task, but may be needed by more than one class (say, the value for gravity in a game), I'll put them in a definition class like that. I dislike functional classes referencing each other simply for constant values (too much coupling).

Upvotes: 0

user562566
user562566

Reputation:

I think your existing method is correct as it is the most logical. Typically you don't see (in common architectures) classes that contain ONLY static/constant variables. You'd typically see them bundled with a class where the functionality that most utilizes these statics exists. For example, any of the Events classes in AS3. You'd see the static const MouseEvent.MOUSE_DOWN etc bundled in with the actual event class (MouseEvent). I think separating them into a package that purely exists to store statics would be less logical, although it wouldn't necessarily be wrong. Anyway if you were to follow the example of something established, I'd look to the AS3 language itself as an example and in that example we can see that the flash core team seems to assemble classes into packages with the highest level of association. I think perhaps your doubt in this is arising because you have a class composed purely of constants and no functionality or inheritance that couples or links the class(es) explicitly to anything else.

Upvotes: 1

Related Questions